ZF-2243: Different behavior between Zend_Db_Table::createRow($data) and Zend_Db_Table_Row::setFromArray(array $data)
Description
Zend_Db_Table::createRow($data) is filtering data that are not part of the column table while Zend_Db_Table_Row::setFromArray($data) is not.
Zend_Db_Table::createRow()
$defaults = array_combine($this->_cols, array_fill(0, count($this->_cols), null));
$keys = array_flip($this->_cols);
$data = array_intersect_key($data, $keys);
$data = array_merge($defaults, $data);
Zend_Db_Table_Row::setFromArray()
foreach ($data as $columnName => $value) {
$this->$columnName = $value;
}
I found filtering a nice feature especially when you pass all the data coming from a Form as an argument.
Comments
Posted by Adam Jensen (jazzslider) on 2008-02-29T13:05:20.000+0000
Glad to see this has been reported; I was just about to post it myself.
I think Zend_Form has made this issue quite a bit more important. My Zend_Form objects frequently include submit button elements, which get returned back as part of the result of Zend_Form::getValues(). This makes it impossible to use that returned array as the parameter of setFromArray().
Maybe, however, instead of changing the behavior entirely, we should make this "filtering" optional via a new argument for setFromArray(). The logic would go like this:
[code] public function setFromArray(array $data, $strict = true) { if ($strict === false) { // filter all non-column elements out of the array } // assign column values as at present } [/code]
This would keep things backwards-compatible.
Posted by Wil Sinclair (wil) on 2008-03-25T20:43:57.000+0000
Please categorize/fix as needed.
Posted by Stephen Crosby (stevecrozz) on 2008-07-22T15:48:33.000+0000
The codebase has changed a little since the initial report, but this issue remains. I've created this patch that moves array_intersect_key($data, $defaults); out of Zend_Db_Table_Abstract::createRow() and into Zend_Db_Table_Row_Abstract::setFromArray() . setFromArray() is already called by createRow() so moving this line should make the two function consistent.
This patch works in a few of my own test cases.
Posted by Jaka Jancar (jaka) on 2008-08-14T09:32:07.000+0000
+1 vote. Stephen's suggestion (moving the logic into Zend_Db_Table_Row) works for me too.
Posted by Jaka Jancar (jaka) on 2008-08-14T09:38:24.000+0000
Additionally, setFromArray()'s description is a bit misleading: ??Sets all data in the row from an array.?? It does not set all data (i.e. set unspecified columns to null), but only the given columns.
Posted by Mickael Perraud (mikaelkael) on 2008-11-01T08:13:42.000+0000
Fixed in SVN12245
Posted by Wil Sinclair (wil) on 2008-11-13T14:09:58.000+0000
Changing issues in preparation for the 1.7.0 release.