Yii 事件:将事件设置为最后一个

问题描述 投票:0回答:1

我有几个事件处理程序(它们是

CActiveRecordBehavior
类)来响应我的
onAfterSave
类的
CActiveRecord
事件。

其中一个事件处理程序需要调用其他事件处理程序才能继续。

我怎样才能强制这个事件成为最后执行的?

我的行为声明如下:

public function behaviors()
{
    return array_merge(
        array(
            'firstBehavior' => array(
                'class' => 'application.components.firstBehavior',
            ),
            'secondBehavior' => array(
                'class' => 'application.components.secondBehavior',
            ),
        ), parent::behaviors()
    );
}

我打电话时就知道

$model->getEventHandlers($eventName)->add($eventHandler);

该事件附加在列表末尾并最后执行,但我现在不知道可以在哪里放置此代码: - 在附加行为之前调用 activeRecord 对象的

init()
方法,因此即使我从此方法添加它而不是通过在列表中添加行为,添加的事件也不会是最后执行的。

php events yii
1个回答
0
投票

Jon 在评论中表示,实现这一目标的最佳方法不是附加事件处理程序来覆盖现有方法:

public function save($runValidation=true,$attributes=null)
{
    if(parent::save($runValidation, $attributes)) {
        //Handle my event since the other events have been executed
        // in the parent::save method
        return true;
    } else {
        return false;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.