【问题标题】:How to translate this query with Doctrine?如何用 Doctrine 翻译这个查询?
【发布时间】:2011-01-05 15:12:35
【问题描述】:
我尝试生成自定义查询(我正在为网站开发搜索引擎)。
这是要翻译的查询:
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
你能帮帮我吗?
【问题讨论】:
标签:
sql
symfony1
doctrine
symfony-1.4
dql
【解决方案1】:
未测试,但类似这样的方法应该可以解决问题:
$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();
...这利用了以下事实,例如:
AND (`id_secteur`=1 OR `id_secteur` = 2 or id_secteur = 3)
...等同于:
AND `id_secteur` IN (1, 2, 3)