Zend framework Zend DbTable add where in if()语句

Zend framework Zend DbTable add where in if()语句,zend-framework,where-clause,zend-db-table,Zend Framework,Where Clause,Zend Db Table,我通过以下方式获取数据: $table = new Application_Model_DbTable_FooBar(); $data = $table ->select() ->where('id = ?', (int) $id) ->query() ->fetchAll(); 并希望在if()语句中添加单独的where子句。 诸如此类: if($foo == "bar") { $data->where('foo = ?, 'bar'); } 所以我

我通过以下方式获取数据:

$table = new Application_Model_DbTable_FooBar();

$data = $table
->select()
->where('id = ?', (int) $id)
->query()
->fetchAll();
并希望在if()语句中添加单独的where子句。
诸如此类:

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();
试试这个

$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();