dousaoxiancy199896 2016-01-07 18:58
浏览 37

Model Observer,不适用于更新?

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

And here, my model class

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));
}
  • 写回答

1条回答 默认 最新

  • dongleiwei2182 2016-01-11 12:40
    关注

    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.

    for more details. https://github.com/laravel/framework/issues/11777#issuecomment-170388067

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题