Hossam Aldeen Ahmed
Hossam Aldeen Ahmed

Reputation: 782

Yii autocall function after execute command like afterSave in active record

I want to trigger a function on every update made in my DB. For example, I can use afterSave() in Yii-activeRecord, but how to use this within command builder? How to do a method to run after execute command ?

Maybe I make a class to extends the CDbCommand and override the execute method like:

public function execute($params=array())
{
    parent::execute($params);
    $this->afterExecute();
}

but i don't know where to put it

Upvotes: 0

Views: 143

Answers (1)

lin
lin

Reputation: 18392

You can handle this by writing a component which extends CDbCommand. You can store your components in "/protected/components", which will be Yii-conform. All you need to do, is make your command DB-part extending CDbCommandExtension. I did that for you:

<?php
/**
 * CDbCommandExtension component 
 *
 * @author lin
 *
 */
class CDbCommandExtension extends CDbCommand {

    ############################################ Class vars ############################################    


    ############################################ Class methods ############################################


    /**
     * On init, init parent
     */
    public function init(){

        //Pattern call
        parent::init();
    }


    /**
     * Override execute mehtod in parent class
     * 
     * (non-PHPdoc)
     * @see CDbCommand::execute()
     */
    public function execute($params=array())
    {
        parent::execute($params);
        $this->afterExecute();
    }


    /*
     * after exectue function
     */
    public function afterExecute() 
    {
        die('after execute');
    }
}
?>

Upvotes: 1

Related Questions