简体   繁体   中英

zend framework: how to create trigger “before/after” insert/update

I am looking for a method that will be called before/after insert() or update() in Zend_DB? I am don't want to relly on database trigger for this ... could you help me ? thank you !!!

Just override the insert() and update() methods in your Table class.

For example:

<?php
class ObjectNameTable extends Zend_Db_Table_Abstract {
    protected $_name = 'table_name'; // table name
    protected $_primary = 'id';

    public function insert(array $data) {
        $data['added'] = date('Y-m-d H:i:s');
        return parent::insert($data);
    }

    public function update(array $data, $where) {
        $data['updated'] = date('Y-m-d H:i:s');
        return parent::update($data, $where);
    }
}

If you want to do it for all your table objects then you could have a base class that they all extend such as:

<?php
class BaseTable extends Zend_Db_Table_Abstract {
    public function insert(array $data) {
        $data['added'] = date('Y-m-d H:i:s');
        return parent::insert($data);
    }

    public function update(array $data, $where) {
        $data['updated'] = date('Y-m-d H:i:s');
        return parent::update($data, $where);
    }
}

And then a class to use it:

<?php
class ObjectNameTable extends BaseTable {
    protected $_name = 'table_name'; // table name
    protected $_primary = 'id';
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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