删除模型上的Laravel递归事件未触发

时间:2018-10-16 07:28:13

标签: php mongodb laravel

我有一个树模型结构。

我正在尝试删除父级,应该使用调度的deleted事件删除所有后代。

我还试图删除所有后代节点的所有图像。

如您所见,我正在使用MongoDb ext(Treetrait)。

我记录了一些消息,以查看是否调度事件被多次调用,但是在删除父模型时它仅被调用一次。

我感觉由于deleted删除事件而未触发调度的mass事件。

有人遇到过这样的事情吗?

我正在使用分机https://github.com/beyondlex/mongotree 且使用分机https://github.com/lazychaser/laravel-nestedset

这是模型:

<?php

namespace App;

use Jenssegers\Mongodb\Eloquent\Model as Model;
use Lex\Mongotree\TreeTrait;

class Department extends Model
{
    use TreeTrait;

    protected $dispatchesEvents = [
        'deleted' => \App\Events\DepartmentDeleted::class,
    ];

}

这是事件:

<?php

namespace App\Events;

use App\Department;

class DepartmentDeleted
{

    public $department;

    public function __construct(Department $department)
    {
        $this->$department = $department;

        \Log::debug(json_encode($department->getAttributes()));

        $destination_path = public_path('/images/');

        if (
            $department->image['en'] != ''
            &&
            file_exists($destination_path . $department->image['en'])
        ) {
            \Log::debug('deleting file en' . $department->image['en']);
            unlink($destination_path . $department->image['en']);
        }

        if (
            $department->image['ar'] != ''
            &&
            file_exists($destination_path . $department->image['ar'])
        ) {
            \Log::debug('deleting file ar' . $department->image['ar']);
            unlink($destination_path . $department->image['ar']);
        }

    }
}

0 个答案:

没有答案