ZF-2360: Additions to Row class
Description
h3. Test for whether or not a Row object isNew I have run into a few situations where I needed to know whether or now the Row object passed already existed in the database. I think the following "isNew" method would be helpful in determining if a Row object is new or a reference to an existing row in the database:
public function setTimestamp(Zend_Db_Table_Row_Abstract $row)
{
if ($row->isNew()) {
$row->created = date('Y-m-d H:i:s');
}
$row->updated = date('Y-m-d H:i:s');
}
\
h3. Getting a Row Object's Modified Data
This also comes out of a use-case scenario where I required an email to be sent when the status of a row table.status was modified. This check was OUTSIDE of the row object so I didn't have direct access to whether or not $Row->status has changed:
$modifiedData = $Row->getModifiedData();
if (in_array('status', $modifiedData)) {
// Send email with the new status
}
\
h3. Code for the examples above
abstract class Zend_Db_Table_Row_Abstract
{
/**
* Returns true if the row is new (no clean/db data)
*
* @return bool
*/
public function isNew()
{
return empty($this->_cleanData);
}
/**
* Returns the diff of the data from the cleanData.
*
* @return array
*/
public function getModifiedData()
{
return array_diff_assoc($this->_data, $this->_cleanData);
}
}
Comments
Posted by Wil Sinclair (wil) on 2008-03-25T20:43:56.000+0000
Please categorize/fix as needed.
Posted by Teemu Välimäki (cred) on 2008-04-01T14:21:12.000+0000
1st case could be dealt with current code
http://framework.zend.com/manual/en/…
2nd case seems to be asking for a trigger and if I'm not too deep in the fantasy land I think there's plugins coming for tapping into triggers or something.
In general I don't think it would be a good idea to implement asked features as proposed.
Posted by Wil Sinclair (wil) on 2008-04-18T13:12:00.000+0000
This doesn't appear to have been fixed in 1.5.0. Please update if this is not correct.
Posted by Coen Hyde (coenhyde) on 2008-07-15T21:54:24.000+0000
Yes this would be good. I have a common parent in all my row classes that implements the function isNewRow( ). I feel 'isNewRow' would be a better name than just i'isNew'.
Re: Teemu Yes but not all custom logic can be done when the row is saved. What happens if you want to apply custom logic when the row is created? The 'isNewRow' comes in handy eg.