【问题标题】:How to trigger an event that fires after record in the database is updated? [OctoberCMS]如何触发数据库中的记录更新后触发的事件? [十月CMS]
【发布时间】:2019-10-01 07:02:37
【问题描述】:

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

我已经在boot函数的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

【问题讨论】:

    标签: octobercms octobercms-backend


    【解决方案1】:

    ExpertsComment 你的课吗?如果是这样,您可以在plugins/foo/bar/models/ExpertsComment.php 中将事件直接添加到您的模型中。不要忘记在ExpertsComment.php 中添加Log Facade,use Log;Here 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!");
                    }
                });
            });
        }
    
        .... 
    }
    

    【讨论】:

    • 谢谢大家,我正在扩展另一个插件,但因为我正在实现代码的第一部分而被卡住了。实现扩展插件的部分完全可行:)
    猜你喜欢
    • 2017-07-18
    • 2016-04-15
    • 1970-01-01
    • 2017-07-22
    • 1970-01-01
    • 1970-01-01
    • 2021-09-27
    • 2016-08-03
    • 1970-01-01
    相关资源
    最近更新 更多