模型观察员,无法进行更新吗? [英] Model Observer, not working for update?

查看:83
本文介绍了模型观察员,无法进行更新吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试使我的模型观察器工作时遇到麻烦.它正在按预期的方式进行创建和删除,但无法进行更新.我猜事件永远不会触发.问题是所有事情都以完全相同的方式完成.有任何想法吗? 下面,我的观察者.

Im having trouble trying to get my model observer to work.. It is working as expected for create and deleted, but not for updating. Im guessing the event never fires. The thing is all of then are being done exactly the same way. Any ideas? Below, my observer.

class GenericObserver extends AbstractObserver {

protected $events;

public function __construct(Dispatcher $dispatcher){
    $this->events = $dispatcher;

}

public function saved($model) {
    dd($this->events);

    $user_id = Auth::user()->usr_id;
    $user_nome = Auth::user()->usr_nome;
    $user_email = Auth::user()->usr_email;

    dd($model);
}

public function deleted($model) {
    $user_id = Auth::user()->usr_id;
    $user_nome = Auth::user()->usr_nome;
    $user_email = Auth::user()->usr_email;

    echo($model->getTable());
    dd($model->getKeyName());

}

public function updated($model) {


    $user_id = Auth::user()->usr_id;
    $user_nome = Auth::user()->usr_nome;
    $user_email = Auth::user()->usr_email;

    dd($model);

}

public function saving($model){
    echo 'Saving';
}

public function deleting($model){
    echo 'Deleting';
}

public function updating($model){
    echo 'Updating';
}

在这里,我的模型课

Aplicacao extends Model {
protected $table = 'gst_aplicacoes';

protected $primaryKey = 'app_id';

protected $fillable = ['app_nome', 'app_key', 'app_observacao'];

public static function table() {
    $model = new static;
    return $model->getTable();
}

 public static function boot() {
    parent::boot();


    Aplicacao::observe(new GenericObserver(new Dispatcher));
}

推荐答案

如果有人遇到此问题,则不触发该事件的原因是因为update方法仅在直接在模型上发生更新时才触发其事件,由于我使用的是中间存储库来表示我的模型,因此无法正常工作.

If anyone ever faces this issue, the reason the event was not firing was because the update method, only fire its events when the update happens directly on the model, since i was using an intermediary repository to represent my model, it wasn't working.

了解更多详情. https://github.com/laravel/framework/issues/11777#issuecomment-170388067

这篇关于模型观察员,无法进行更新吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆