2013-09-30 11 views
0

我通过这种方式让我的数据:Zend的DBTABLE加入其中if()语句

$table = new Application_Model_DbTable_FooBar(); 

$data = $table 
->select() 
->where('id = ?', (int) $id) 
->query() 
->fetchAll(); 

,想添加一个单独的where子句中的if()语句。
类似的东西:

if($foo == "bar") { 
    $data->where('foo = ?, 'bar'); 
} 

所以,我想类似的东西,但没有奏效。

$table = new Application_Model_DbTable_FooBar(); 

$table 
->select() 
->where('id = ?', (int) $id); 

if($foo == "bar") { 
    $table->where('foo = ?, 'bar'); 
} 

$data = $table->query()->fetchAll(); 

回答

1

试试这个

$table = new Application_Model_DbTable_FooBar(); 

$table = $table 
->select() 
->where('id = ?', (int) $id); 

if($foo == "bar") { 
    $table = $table->where('foo = ?, 'bar'); 
} 

$data = $table->query()->fetchAll(); 
+0

完美!谢谢你! – StinsonMaster