Tahir
Tahir

Reputation: 511

Is there any beforeSave or afterSave methods in Zend framework model?

Is there any beforeSave or afterSave methods in Zend framework models?

class VendorReject extends Zend_Db_Table
{
    protected $_name = 'VendorRejects';
}

Upvotes: 0

Views: 224

Answers (2)

Tahir
Tahir

Reputation: 511

Another easy way is to override Models insert/update methods.

Before save example code:

class VendorReject extends Zend_Db_Table
{
    protected $_name = 'VendorRejects';

    // Override parent method
    public function insert(array $data)
    {
       // Add your code here that will execute before insert operation

        return parent::insert($data);
    }
}

After save example code:

class VendorReject extends Zend_Db_Table
{
    protected $_name = 'VendorRejects';

    // Override parent method
    public function insert(array $data)
    {
        parent::insert($data);

        // Add your code here that will execute after insert operation

        return;

    }
}

Upvotes: 0

Daniel Gadawski
Daniel Gadawski

Reputation: 1923

You can override _insert() and _postInsert() methods of Zend_Db_Table_Row_Abstract.

So create row class, for example:

class Row_VendorReject extends Zend_Db_Table_Row
{
    protected function _insert()
    {
        $rejectionDate = $this->rejection_date;
        // do something here

        parent::_insert();
    }

    protected function _postInsert()
    {
        parent::_postInsert();

        // some postprocessing
    }
}

Then fill _rowClass field in your model with new class name:

class VendorReject extends Zend_Db_Table
{
    protected $_name = 'VendorRejects';
    protected $_rowClass = 'Row_VendorReject';
}

Now, each time you invoke save() on row, those methods will be invoked too (before and after insert/update).

If you need this kind of feature with update, there are also _update() and _postUpdate() methods.

Upvotes: 2

Related Questions