ZF-2778: Error in Zend_Db_Table::find() & Zend_Filter_Inflector::filter() not initiating empty array() b4 setting value
Description
PHP acts unpredictably when arrays are not initiated. Any array variable should be set to array() before setting any keys, ex. $my_var[1] = "foo" should be preceded with a $my_var = array(). I've found two errors based on this in 1.5RC1
In Zend_Db_Table::find() the following section should be changed:
$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) {
$whereList[$i][$keyPosition] = $keyValues[$i];
}
}
to:
$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];
}
}
On my computer the "$whereList[$i][$keyPosition] = $keyValues[$i];" creates a semi-random array that contains 3 elements instead of one element causin a annoying warning.
The same error seems to occur in Zend_Filter_Inflector::filter():
$processedParts = array(); // Add this row before the foreach statement
foreach ($this->_rules as $ruleName => $ruleValue) {...}
Comments
Posted by Wil Sinclair (wil) on 2008-03-25T20:43:56.000+0000
Please categorize/fix as needed.
Posted by Simon Mundy (peptolab) on 2008-05-30T18:54:53.000+0000
Resolved in r9577
Posted by Wil Sinclair (wil) on 2008-09-02T10:39:22.000+0000
Updating for the 1.6.0 release.