Zend framework findManyToManyRowset with condition on 3rd column
Is there a way I can do a condition on the join table with a many to many in ZF ?
For example I hav开发者_运维技巧e table A | A_B |B
And B has columns A_ID | B_ID | PUBLISHED
$model = new Model_DbTable_A();
$select = $model->select()->where('id = ?', $id);
$row = $model->fetchRow($select);
$b = $row->findManyToManyRowset('Model_DbTable_B', 'Model_DbTable_AB');
You can pass a Zend_Db_Table_Select
object as the fifth parameter to findManyToManyRowset()
. In the final select statement, intersection table is aliased as i
. So you can set a condition on the intersection table in such a way:
$select = $this->select()
->where( 'i.published = ?', true );
$b = $row->findManyToManyRowset(
'Model_DbTable_B', 'Model_DbTable_AB',
$callerRefRule, $matchRefRule,
$select);
The apparent downside of this approach is that it's relying on Zend_Db_Table_Row_Abstract
internals too much and, therefore, does not look clear unless peeking into findManyToManyRowset()
definition.
What about something like this (this is slight modification of the answer by Vika):
$model = new Model_DbTable_A();
$row = $model->find($id)->current(); // lite shortcut to get $row by $id
// get select object
$select = $model->select()->where('i.published = ?', true);
// fetch rows in B through A_B table where A_B.published is true
$b = $row->findManyToManyRowset('Model_DbTable_B', 'Model_DbTable_AB', null, null, $select);
精彩评论