• Eloquent Observer 的小坑


    前言

    最近开发新的项目不是基于完整的 laravel 框架,框架是基于 laravel ORM 的简单MVC模式。随着项目的成熟和业务需求,需要引入事件机制。简单地浏览了一下 symfony、laravel 的事件机制,都是依赖于 container 的。感觉如果引入的话开销有点大,在寻觅更好解决方案过程中发现 ORM 居然自带事件机制,完全足够目前的业务需求,又不需要改动框架,简直不能太棒!这里简单的记录一下orm observe 的使用吧。

    事件触发流程

    由于 ORM 事件是针对 Model 的,所以本身结合 Model 封装了一些基础的事件。感觉基本上是够使用了,如果需要添加额外事件的话,ORM 也提供了 setObservableEvents()供使用。

    public function getObservableEvents()
        {
            return array_merge(
                [
                    'creating', 'created', 'updating', 'updated',
                    'deleting', 'deleted', 'saving', 'saved',
                    'restoring', 'restored',
                ],
                $this->observables
            );
        }

    我们都知道 ORM 无论是 create 还是 update 本质是 save。因此分析一下 ORM save 时底层流程是:

    public function save(array $options = [])
        {
            $query = $this->newQueryWithoutScopes();
            //触发 saving 事件
            if ($this->fireModelEvent('saving') === false) {
                return false;
            }
            if ($this->exists) {
                $saved = $this->performUpdate($query, $options);
            }
            else {
                $saved = $this->performInsert($query, $options);
            }
    
            if ($saved) {
                $this->finishSave($options);
            }
            return $saved;
        }
    
    protected function performInsert(Builder $query, array $options = [])
        {
            //触发 creating 事件
            if ($this->fireModelEvent('creating') === false) {
                return false;
            }
            if ($this->timestamps && Arr::get($options, 'timestamps', true)) {
                $this->updateTimestamps();
            }
            $attributes = $this->attributes;
            if ($this->getIncrementing()) {
                $this->insertAndSetId($query, $attributes);
            }
         else {
                $query->insert($attributes);
            }
            $this->exists = true;
            $this->wasRecentlyCreated = true;
            //触发 created 事件
            $this->fireModelEvent('created', false);
            return true;
            }
    
    protected function finishSave(array $options)
        {
            //触发 saved 事件
            $this->fireModelEvent('saved', false);
            $this->syncOriginal();
            if (Arr::get($options, 'touch', true)) {
                $this->touchOwners();
            }
        }

    以上是阐述 creat 时事件触发流程,显而易见依次是 saving>creating>created>saved。

    update 同理就不具体贴代码了, saving>updating>updated>saved。

    而 delete 不涉及 save,因此依次只触发了 deleting 和deleted。 当 restore 软删除记录时触发了 restoring 和 restored 方法。

    使用

    只需在 model 添加 boot 方法,可以直接在 boot 方法中定义事件,也可以面向于 model 新建 modelObserver 集中处理对应的 model 事件。

     public static function boot()
        {
            parent::boot();
            static::setEventDispatcher(new IlluminateEventsDispatcher());
            //static::saving(function($model){  });
            static::observe(new testObserver());
        }
    
    class testObserve{
     public function saving($model)
        {
            $model->name=name.'test';
        }
    }

    大坑

    不得不感叹这个事件真的既简单又实用,但是!当我在开发过程中有一处 update 怎么都不会触发事件,捉急的很啊。菜鸟一下看不出问题本质,只能打断点一点点排查。发现是因为该处代码前调用了belongsToMany()

    • 痛点1:dispatcher 与 container 共生死单例。
      多对多模型关联 boot 两个model,因此 boot model2 时,将 dispatcher 指向了 model2。
    • 痛点2:model 只 boot 一次
      bootIfNotBooted(),booted model 不会重新调用 boot 方法。

    • 结果:update model1 怎么都无法触发事件。
    • 解决:目前能想到的便是在调用 belongsToMany()时,调用 ORM 的 clearBootedModels()方法,清除 bootedModel,update model1 时再次 boot model1,强行将 dispatcher 指向 model1.
  • 相关阅读:
    vc 网络编程(socket)
    《精通Windows API-函数、接口、编程实例》——第4章文件系统
    WindowsAPI使用详解——GetVersion|GetVersionEx 获取操作系统版本和名称
    Windows编程 鼠标
    Windows 编程 键盘
    Windows编程 Windows程序的生与死(下)
    二叉查找树——A1043.Is it a Binary Search Tree(25)
    Springboot + ElasticSearch 构建博客检索系统
    树的遍历——A1053.Path of Equal Weight(30) 只可DFS不可BFS
    树的遍历——A1004.Counting Leaves(30) 给出一棵树,问每一层有多少叶子节点(可DFS也可BFS)
  • 原文地址:https://www.cnblogs.com/mouseleo/p/8667922.html
Copyright © 2020-2023  润新知