如何在Laravel 5.4中使用存储库模式实现事件/侦听器

时间:2019-04-15 22:41:03

标签: laravel design-patterns listener repository-pattern

当我使用个人信息存储库时,我无法使侦听器触发操作更新,创建或删除。

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

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');
    }
}

预先感谢。预先感谢。预先感谢。预先感谢。预先感谢。预先感谢

1 个答案:

答案 0 :(得分:0)

以下是从docs中摘录的相关内容(滚动到批量更新):

  

通过Eloquent发布批量更新时,不会为更新的模型触发savedupdated模型事件。这是因为发布批量更新时从未真正检索过模型。

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

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());
});