I have this table class:
class Songs extends Zend_Db_Table_Abstract
{
protected $_name = 'songs';
protected $_primary = 'song_id';
protected $_rowClass = 'Song';
}
And a class that extends the class above with some custom logic.
class Song extends Zend_Db_Table_Row_Abstract
{
protected function _insert()
{
print_r($this);
// $this does exist
}
protected function _update()
{
print_r($this);
//$this does not existing when updating a row, why not?
}
}
My problem is that when I’m inserting a new row I can use $this in my custom logic.
$row->save(); // $this exists in _insert()
But it doesn’t exist when I’m trying to update a row.
$myRow->update($data, $where); // $this does not exists in _update()
Why does $this not exist when I want to do some custom logic before updating a row?
To update a row, you don’t use:
You use:
But trying to use
update()on a row object should throw an exception.So I’m guessing you’re actually calling the
update()function on the table object, and not the row object.At that point the row object is never even used, the query is simply generated from the
$dataarray and the$whereclause.If you want to use the custom
_update()method you would need to do something like:Of course is also perfectly valid to add custom logic at the table level, and should be noted while calling an update or insert from the table object does not use the row object, calling
save()on the row object proxies the table object.For example, from the
Zend_Db_Table_Row_doInsert()function:So if you have custom logic that you want to use every time you update a row (whether you update from the table object or the row object), it should be put into the table object.
From the Zend_Db_Table_Row docs: