简体   繁体   English

Zend框架模型中是否有任何beforeSave或afterSave方法?

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

Is there any beforeSave or afterSave methods in Zend framework models? Zend框架模型中是否有任何beforeSave或afterSave方法?

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

You can override _insert() and _postInsert() methods of Zend_Db_Table_Row_Abstract. 您可以重写_insert()_postInsert()方法。

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: 然后用新的类名填充模型中的_rowClass字段:

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). 现在,每次在行上调用save()时,这些方法也会被调用(在插入/更新之前和之后)。

If you need this kind of feature with update, there are also _update() and _postUpdate() methods. 如果您需要具有更新功能的此类功能,则还有_update()_postUpdate()方法。

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;

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM