2010-01-12 44 views
0

讓我們說,我有:Zend框架的查詢,添加到字符串

$query = $this->select() 
       ->where('name LIKE ?','%something%'); 
       ->where('section LIKE ?','%something%'); 
       ->where('cars LIKE ?','%something%'); 
       ............ 
       ............ 

你明白了吧!

但我想添加where()子句的查詢只讓我們說如果.. $ x = 0;像這樣:

$select = $this->select(); 
if($x == 0): 
//add to the $this->select(); so it will be $this->select()->where('section LIKE ?','%something%'); 
$select->where('section LIKE ?','%something%'); 
endif; 

if($x == 0): 
/*add to the $this->select(); so it will be 
$this->select()->where('section LIKE ?','%something%') 
->where('cars LIKE ?','%something%'); 
*/ 
$select->where('cars LIKE ?','%something%'); 
endif; 

當然,這個東西不起作用,但我想你已經明白我想做什麼了!

最好的問候,

回答

2

您是否考慮過事先構建SQL查詢字符串manualy,然後在$ db引用上調用fetchAll()?

$SQL = 'SELECT * from blah, '; 
if($x == 0): 
    //add to the $this->select(); so it will be $this->select()->where('section LIKE ?','%something%'); 
    $SQL = $SQL . "WHERE blah"); 
endif; 

if($x == 1): 
    $SQL = $SQL . "WHERE blah"); 
endif; 

$db = Zend_Db_Table::getDefaultAdapter(); 
$this->view->leaguetable = $db->fetchAll($SQL); 

的Zend_DB.select()返回一個SQL查詢語句

$select = $this->select(); 

,但你不能有條件地控制添加的where子句的select()方法後,該對象已完成。這不是一個附加操作。

$select = $this->select(); 
if($x == 0): 
$select->where('section LIKE ?','%something%'); 
endif; 
+0

創建查詢格式其實,你可以使用該代碼$選擇 - >其中()在操作中添加更多。 – Chris 2010-01-13 00:55:45

+0

是啊我知道,那就是我所做的,我有一個錯誤..那就是爲什麼不工作! – Uffo 2010-01-13 01:30:35

0

問題似乎是你的if語句,而不是

$x = 0 

你想

$x == 0 

前者是如果子句中指定0給$ x,這幾乎從來不是你想要的。

+0

srry,這是一個tipo..but仍不能做我想做的事......它需要爲我 – Uffo 2010-01-12 23:19:37