douzhangcuo2174 2014-08-21 12:33
浏览 197
已采纳

执行命令后的Yii自动调用功能,如活动记录中的afterSave

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

  • 写回答

1条回答 默认 最新

  • douchong4730 2014-08-21 14:20
    关注

    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');
        }
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试