Possible to use _referenceMap with join() on Zend_Db_Select?
Example:
class Products extends Zend_Db_Table_Abstract
{
protected $_name = 'products';
protected $_referenceMap = array(
'Bug' => array(
'columns' => array('bug_id'),
'refTableClass' => 'Bugs',
'refColumns' => array('bug_id')
)
);
}
$object = new Products();
$select = $object->select()开发者_开发技巧->from()->Join('Bug');
Instead of defining the full join statement
As far as I can tell $_referenceMap is not used in that way. $_referenceMap defines the relationship that a table row has with other tables.
That's why the associated findDependentRowset(), findManyToManyRowset() and findParentRow() are found in Zend_db_Table_Row_Abstract. These methods create the Joins.
So to get the dependent rows from Bugs, using a select object, you would do something like this, assuming that Products has a one-to-many relationship with Bugs;
class Products extends Zend_Db_Table_Abstract
{
protected $_name = 'products';
protected $_dependentTables = array('Bugs');
}
class Bugs extends Zend_Db_Table_Abstract
{
protected $_referenceMap = array(
'Products' => array(
'columns' => array('bug_id')
,'refTableClass' => 'Products'
,'refColumns' => array('bug_id')
)
);
}
To get dependent rows you first have to fetch a parent row.
$products = new Products();
$productRow = $products->find(123)
->current();
You can refine the join by using Zend_Db_Select
$select = $products->select()
->where('foo_bar = ?', 'cheese')
->limit(2);
Finally querying the dependent rows by passing in the select object instead instead of a rule key.
$bugRowset = $productRow->findDependentRowset('Bugs', 'Products', $select);
I think this will work, I'll have to check tomorrow morning.
This is usefull for one row, but not for the whole table (or several rows). I usually need queries affecting more than one row... Zend should implement the option mentioned by Phliplip, or something similar:
$select = $object->select()->from()->Join('Bug');
Note: I mean, using only one query
精彩评论