How to translate this query with Doctrine?
-
25-09-2019 - |
Question
I try to generate a custom query (I'm developing a search engine for a website).
This is te query to translate :
SELECT * FROM `offre_habitation`
WHERE `id_type_offre` = 2
AND `id_nature_offre` = 1
AND (`nb_pieces` = 2 or `nb_pieces` = 1 or `nb_pieces` = 3 or `nb_pieces` = 4)
AND (`id_secteur`=1 OR `id_secteur` = 2 or id_secteur = 3)
AND `surface_habitable` > 90
AND `prix` > 700
Could you help me please ?
Solution
Not tested, but something like this should do the trick:
$q = Doctrine_Query::create()
->select('o.*')
->from('offre_habitation o')
->where('o.id_type_offre = ?', 2)
->andWhere('o.id_nature_offre = ?', 1)
->andWhereIn('o.nb_pieces', array(1, 2, 3, 4))
->andWhereIn('o.id_secteur', array(1, 2, 3))
->andWhere('o.surface_habitable > ?', 90)
->andWhere('o.prix > ?', 700);
// Test:
echo $q->getSqlQuery();
...this makes use of the fact that, for example:
AND (`id_secteur`=1 OR `id_secteur` = 2 or id_secteur = 3)
...is the same as:
AND `id_secteur` IN (1, 2, 3)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow