Laravel模型事件不会触发

时间:2017-07-29 16:00:24

标签: php laravel

我在laravel 5.4中制作了一个自定义特征,所以当一个数据透视表是->sync();时,它会触发一个更新事件,然后我会用一个观察者来听。

然而,它不按我想要的方式工作。

在我的应用服务提供商中:

Organisation::observe(OrganisationObserver::class);

我有这个:

/**
 * Listen to the Organisation updated event.
 *
 * @param Organisation $organisation
 * @return void
 */
public function updated(Organisation $organisation)
{
    dd('test');
    $this->cache->tags(Organisation::class)->flush();
    $this->cache->tags(Relation::class)->flush();
}

这是我在模特身上使用的特性:

<?php

namespace App\Deal\Custom;

trait BelongsToManyWithSyncEvent
{
    /**
     * Custom belongs to many because by default it does not observes
     * changes on pivot tables. When a pivot table is changed return
     * a custom belongs to many with sync cache clear class.
     * Here we will clear the cache.
     *
     * @param $related
     * @param null $table
     * @param null $foreignKey
     * @param null $relatedKey
     * @param null $relation
     * @return BelongsToManyWithSyncCacheClear
     */
    public function belongsToMany($related, $table = null, $foreignKey = null, $relatedKey = null, $relation = null)
    {
        if (is_null($relation)) {
            $relation = $this->guessBelongsToManyRelation();
        }

        $instance = $this->newRelatedInstance($related);
        $foreignKey = $foreignKey ?: $this->getForeignKey();
        $relatedKey = $relatedKey ?: $instance->getForeignKey();

        if (is_null($table)) {
            $table = $this->joiningTable($related);
        }

        return new BelongsToManyWithSyncCacheClear(
            $instance->newQuery(), $this, $table, $foreignKey, $relatedKey, $relation
        );
    }
}

BelongsToManyWithSyncCacheClear类看起来像这样:

<?php

namespace App\Deal\Custom;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Concerns\HasEvents;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;

class BelongsToManyWithSyncCacheClear extends BelongsToMany
{
    use HasEvents;

    /**
     * BelongsToManyWithSyncEvents constructor.
     *
     * @param Builder $query
     * @param Model $parent
     * @param string $table
     * @param string $foreignKey
     * @param string $relatedKey
     * @param null $relationName
     */
    public function __construct(Builder $query, Model $parent, $table, $foreignKey, $relatedKey, $relationName = null)
    {
        parent::__construct($query, $parent, $table, $foreignKey, $relatedKey, $relationName);
    }

    /**
     * When a pivot table is being synced,
     * we will clear the cache.
     *
     * @param array|\Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection $ids
     * @param bool $detaching
     * @return array
     */
    public function sync($ids, $detaching = true)
    {
        $changes = parent::sync($ids, $detaching);

        $this->fireModelEvent('updated', false);

        return $changes;
    }
}

在这里我发布了一个更新的事件:

$this->fireModelEvent('updated', false);

但我的dd('test');中的OrganisationObserver永远不会被触发。我在这做错了什么!?

0 个答案:

没有答案