如何触发更新数据库中的记录后触发的事件? [OctoberCMS]

时间:2019-09-28 14:07:59

标签: octobercms octobercms-backend

我一直在尝试触发一个事件,该事件告诉我记录中的特定信息是否已更改,但是我无法执行(不会给出任何错误,但不会记录我期望的信息)

我已经在启动功能的Plugin.php文件中添加了它

        $comment =  new ExpertsComment;
        $comment->bindEvent('model.afterUpdate', function () use (\October\Rain\Database\Model $model) {
            if ($model->is_approved !== $model->original['is_approved']) {
              Log::info("Event Fired!");
         }
        });

从这里https://octobercms.com/docs/api/model/afterupdate

1 个答案:

答案 0 :(得分:1)

专家评论您的课程吗?如果是这样,您可以在plugins/foo/bar/models/ExpertsComment.php中将事件直接添加到模型中。不要忘记在use Log;中添加日志外观ExpertsComment.phpHere are documents for model event basic usage.

public function afterUpdate() 
{
    if ($this->is_approved !== $this->original['is_approved']) {
        Log::info("Event Fired!");
    }
}

或者,如果您想扩展另一个类,则可以在您的一个插件类中完成,即:plugins/foo/bar/Plugin.phpHere are the documents for extending a class.

use Foo\Bar\Models\ExpertsComment;

class Plugin extends PluginBase
{
    ....

    public function boot()
    {
        ExpertsComment::extend(function($model) {
            $model->bindEvent('model.afterUpdate', function() use ($model) {
                if ($model->is_approved !== $model->original['is_approved']) {
                    Log::info("Event Fired!");
                }
            });
        });
    }

    .... 
}