2011-01-05 26 views
0

我嘗試生成一個自定義查詢(我正在開發一個網站的搜索引擎)。如何翻譯這個查詢與教條?

這是TE查詢翻譯:

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 

你能幫助我嗎?

回答

7

沒有測試,但是這樣的事情應該做的伎倆:

$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) 
+0

這似乎工作,謝謝! – bahamut100 2011-01-05 16:26:29

+1

+1 ...我也會把「prix」變成「o.prix」。 – Tom 2011-01-05 20:22:00

+0

@湯姆發現了。改變。 – 2011-01-06 09:32:08