Eloquent 模型恢复时忽略更新事件

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

我正在构建一个 package 来记录 eloquent 上发生的更改,并且我正在尝试找出要检查的内容以在

updated
时忽略
restored
事件。

trait HasLogs
{
    public static function bootHasLogs(): void
    {
        // created ...
        self::updated(callback: fn ($model) => self::log($model, 'updated'));

        self::deleted(callback: function ($model) {
            // Ignore 'deleted' event when 'softDeleted' or 'forceDeleted'
            if (in_array('Illuminate\Database\Eloquent\SoftDeletes', (class_uses(self::class)))) {
                return;
            }
            self::log($model, 'deleted');
        });

        if (in_array('Illuminate\Database\Eloquent\SoftDeletes', (class_uses(self::class)))) {
            // softDeleted ...
            self::restored(callback: fn ($model) => self::log($model, 'restored'));
            // forceDeleted ...
        }
    }
    // ...
}

以下是操作顺序(如果有帮助的话):

  1. Model::restore()
    关于
    SoftDeletes
    特质。
  2. ----
    restoring
    被解雇了。
  3. ----
    Model::save()
  4. --------
    saving
    已被解雇。
  5. --------
    Model::performUpdate()
  6. ------------
    updating
    已被解雇。
  7. ------------
    Builder::update()
  8. ------------
    updated
    已被解雇
  9. --------
    Model::finishSave()
  10. ------------
    saved
    已被解雇。
  11. ----
    restored
    被解雇了。
laravel events eloquent event-handling laravel-9
2个回答
0
投票

特征

HasLogs
Model
添加了一个布尔属性,名为
loggableIsBeingRestored
,默认设置为
false

该特征还为

restoring
注册了一个新的侦听器,在请求所描述的操作时将
loggableIsBeingRestored
设置为
true

然后

updated
侦听器会在继续执行其操作之前检查
loggableIsBeingRestored

trait HasLogs
{
    public bool $loggableIsBeingRestored = false;

    public static function bootHasLogs(): void
    {
        // ...

        self::updated(callback: function ($model) {
            if (isset($model->loggableIsBeingRestored) && $model->loggableIsBeingRestored) {
                // This is a restored event so don't log!
                return;
            }

            self::log($model, 'updated');
        });

        // ...

        self::restored(callback: function ($model) {
            self::log($model, 'restored');

            $model->loggableIsBeingRestored = false;
        });

        self::restoring(callback: function ($model) {
            $model->loggableIsBeingRestored = true;
        });
    }
    // ...
}

0
投票

有很多更直接的方法可以解决这个问题。其中之一是使用观察者:

<?php

namespace App\Observers\User;

use App\Models\User;

class UserObserver {

    /**
     * Handle the User "created" event.
     */
    public function created(User $user): void {
        // do work
    }

    /**
     * Handle the User "updated" event.
     */
    public function updated(User $user): void {

        if (!$user->wasChanged('deleted_at')) { // <- ignore when this field changes
            // do work
        }

    }

    /**
     * Handle the User "deleted" event.
     */
    public function deleted(User $user): void {
        // do work
    }

    /**
     * Handle the User "restored" event.
     */
    public function restored(User $user): void {
        // do work
    }

}
© www.soinside.com 2019 - 2024. All rights reserved.