如何在 laravel 5.4 中使用存储库模式实施 event/listeners

How to implement event/listeners with repository pattern in laravel 5.4

当我使用 patter 存储库时,我无法让侦听器触发操作更新、创建或删除。

此外,我添加了代码以帮助我解决问题。

TicketController.php

namespace App\Http\Organizer\Controllers;

use App\Http\Controllers\Controller;
use App\Http\Events\Contracts\IEvent;
use App\Entities\Event;

class TicketController extends Controller
{
    protected $IEvent;

    public function __construct( IEvent $IEvent )
    {
        $this->IEvent = $IEvent;
    }

    public function checkFutbolType ($activityId)
    {   
       // I need to listen this action here
        $event = $this->IEvent->update(21927, ['title'=>'new title']);
    }

}

我的RepoEvent.php:

<?php

namespace App\Http\Events\Repositories;

use App\Http\Events\Contracts\IEvent
;

class RepoEvent implements IEvent
{

    protected $model;

    public function __construct($model)
    {
        $this->model = $model;
    }

    public function update($activityId, $params)
    {
        return $this->model->where('id', $activityId)->update($params);
    }

}

我的AppServiceProvider.php

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use App\Entities\Event;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        //event: creating
        Event::creating(function (Event $event) {
            return $event->creatingEvent();
        });

        //event: saving
        Event::saving(function (Event $event) {
            return $event->savingEvent();
        });

        //event: updating
        Event::updating(function (Event $event) {
            return $event->updatingEvent();
        });
    } 
}

我的界面IEvent.php:

<?php

namespace App\Http\Events\Contracts;

interface IEvent
{
    public function update($activityId, $params);
}

我的ServicesOrchestration.php:

<?php
namespace App\Http\Administration\Providers;

use App\Entities\Event;

use App\Http\Administration\Repositories\RepoEvent;

use Illuminate\Support\ServiceProvider;

class ServicesOrchestration extends ServiceProvider
{
    public function boot()
    {

    }

    public function register()
    {
        $this->app->bind('App\Http\Administration\Contracts\IEvent', function () {
            return new RepoEvent(new Event());
        });
    }
}

我的模型Event.php

<?php

namespace App\Entities;

use Illuminate\Database\Eloquent\Model;

class Event extends Model
{   
    public function creatingUser() {
        \Log::info('creating event');
    }

    public function savingUser() {
        \Log::info('saving event');
    }

    public function updatingUser() {
        \Log::info('updating event');
    }
}

提前在advance.thanks在advance.thanks在advance.thanks在advance.thanks在advance.thanks感谢

这是从 docs(滚动到大量更新)中截取的相关内容:

When issuing a mass update via Eloquent, the saved and updated model events will not be fired for the updated models. This is because the models are never actually retrieved when issuing a mass update.

为了让您的代码正常工作,您需要先检索实际的模型实例,如下所示:

public function update($activityId, $params)
{
    $instance = $this->model->find($activityId);
    $instance->fill($params);
    $instance->save();
}

执行两个查询而不是一个查询并且一次只能更新一个模型会产生额外成本。

旁注:您正在将模型实例传递到存储库,但您真正想要的是传递查询构建器实例:

$this->app->bind('App\Http\Administration\Contracts\IEvent', function () {
    return new RepoEvent(Event::query());
});