ZF-2815: Add table name into Zend_Db_Table::find() and Zend_Db_Table_Row::_getWhereQuery()
Description
I would like to see that the Zend_Db_Table::find() and Zend_Db_Table_Row::_getWhereQuery() functions have the table name added by default in the where-query. This would allow seemless joins where the table primary keys happen to be the same (like id) without any errors. Personally I'm using this function in my home-made ORM which actually works really nice without changing that much...
I've right now subclassed the functions but I don't see any good reason why not add {quote}$this->_db->quoteTableAs($this->_name) . "." {quote} by default. The functions look like following:
public function find()
{
$args = func_get_args();
$keyNames = array_values((array) $this->_primary);
if (count($args) < count($keyNames)) {
require_once 'Zend/Db/Table/Exception.php';
throw new Zend_Db_Table_Exception("Too few columns for the primary key");
}
if (count($args) > count($keyNames)) {
require_once 'Zend/Db/Table/Exception.php';
throw new Zend_Db_Table_Exception("Too many columns for the primary key");
}
$whereList = array();
$numberTerms = 0;
foreach ($args as $keyPosition => $keyValues) {
// Coerce the values to an array.
// Don't simply typecast to array, because the values
// might be Zend_Db_Expr objects.
if (!is_array($keyValues)) {
$keyValues = array($keyValues);
}
if ($numberTerms == 0) {
$numberTerms = count($keyValues);
} else if (count($keyValues) != $numberTerms) {
require_once 'Zend/Db/Table/Exception.php';
throw new Zend_Db_Table_Exception("Missing value(s) for the primary key");
}
for ($i = 0; $i < count($keyValues); ++$i) {
if (!is_array($whereList[$i])){
$whereList[$i] = array();
}
$whereList[$i][$keyPosition] = $keyValues[$i];
}
}
$whereClause = null;
if (count($whereList)) {
$whereOrTerms = array();
foreach ($whereList as $keyValueSets) {
$whereAndTerms = array();
foreach ($keyValueSets as $keyPosition => $keyValue) {
$type = $this->_metadata[$keyNames[$keyPosition]]['DATA_TYPE'];
$whereAndTerms[] = $this->_db->quoteInto(
$this->_db->quoteTableAs($this->_name) . "." . $this->_db->quoteIdentifier($keyNames[$keyPosition], true) . ' = ?',
$keyValue, $type);
}
$whereOrTerms[] = '(' . implode(' AND ', $whereAndTerms) . ')';
}
$whereClause = '(' . implode(' OR ', $whereOrTerms) . ')';
}
return $this->fetchAll($whereClause);
}
protected function _getWhereQuery($useDirty = true)
{
$where = array();
$db = $this->_getTable()->getAdapter();
$primaryKey = $this->_getPrimaryKey($useDirty);
$info = $this->_getTable()->info();
$metadata = $info[Zend_Db_Table_Abstract::METADATA];
// retrieve recently updated row using primary keys
$where = array();
foreach ($primaryKey as $columnName => $value) {
$column = $db->quoteIdentifier($columnName, true);
$type = $metadata[$columnName]['DATA_TYPE'];
$where[] = $db->quoteIdentifier($info[Zend_Db_Table_Abstract::NAME]) . "." . $db->quoteInto("$column = ?", $value, $type);
}
return $where;
}
Comments
Posted by Wil Sinclair (wil) on 2008-03-25T20:43:52.000+0000
Please categorize/fix as needed.
Posted by Simon Mundy (peptolab) on 2008-06-19T00:22:41.000+0000
Resolved in trunk r9725
Posted by Wil Sinclair (wil) on 2008-09-02T10:39:34.000+0000
Updating for the 1.6.0 release.