【问题标题】:Model event not fired模型事件未触发
【发布时间】:2014-08-15 01:57:06
【问题描述】:

我无法触发模型事件。

目前我有以下

use Illuminate\Database\Eloquent\SoftDeletingTrait;

class ProjectTwitterStatus extends Eloquent {

    use SoftDeletingTrait;

    protected $dates = ['deleted_at'];

    protected $table = 'project_twitter_statuses';

    protected $guarded = array('id');

    public function twitterStatus() {
        return $this->belongsTo('TwitterStatus');
    }

    public function twitterStatusHashtag() {
        return $this->hasMany('TwitterStatusHashtag','twitter_status_id','twitter_status_id');
    }

    public function project() {
        return $this->belongsTo('Project');
    }

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

        static::deleting(function($model)
        {
            echo 'deleting status';
        });
    }
}

并且删除是在辅助类中发起的:

public static function deleteStatus($input_ids, $project) {

    // Make sure ids are in an array
    $twitterStatusIds = (!is_array($input_ids) ? array($input_ids) : $input_ids);

    foreach($twitterStatusIds as $twitterStatusId) 
    {
        ProjectTwitterStatus::where('twitter_status_id', '=', $twitterStatusId)
            ->where('project_id','=',$project->id)
            ->delete();
    }
    // Forget the cache variable
    Cache::forget('twitter-dashboard-statistics-'.$project->id);

    return true;
}

删除工作正常,但不知何故模型事件没有被触发。 “删除状态”的回声是一个占位符,用于显示被触发的事件,我尝试了几个占位符,dd($model) 或创建一个文件都不起作用。所以我假设事件没有被触发。对这件事有什么看法吗?

【问题讨论】:

  • 只是预感,尝试删除SoftDeletingTrait 并尝试一下。我想知道它是否是软删除,删除事件没有被调用。可能是一个错误。
  • 我删除了SoftDeletingTrait,但没有效果:(

标签: events laravel model laravel-4


【解决方案1】:

我知道这不是一个完美的答案,但是我通过使用这个解决了一个类似的问题:

Event::listen('eloquent.creating: ProjectTwitterStatus', function($model)

【讨论】:

  • 我刚试过这个,使用eloquent.deleting,但它不起作用:(。
  • 模型是否从数据库中删除?
  • 是的,它已从数据库中软删除。只有事件没有被触发。
  • 你能在活动结束时试试return false吗?还是删了吗?
【解决方案2】:

我在使用intervention\image 包时再次遇到了这个问题。我将它添加到别名数组中,名称为ImageEditor 而不是Image。然后我创建了一个使用 ImageEditor 的类Image。在干预\图像包的小更新中,我的事件停止触发。我尝试编写类的完整路径,使用static:: 而不是Image::,但它们都不起作用。

现在我将别名改回 Image 并将我的 Image 类重命名为其他名称,事件再次开始触发。

【讨论】:

    【解决方案3】:

    我有/遇到过类似的问题。我用过:

    model0hasmanyrelation()->delete()
    

    虽然记录被(软)删除,但模型的事件没有触发。解决方案是使用:

    model0hasmanyrelation->each(function($model0){$model0->delete()});
    

    【讨论】:

      【解决方案4】:

      你应该改变这个:

      foreach($twitterStatusIds as $twitterStatusId) 
        {
            ProjectTwitterStatus::where('twitter_status_id', '=', $twitterStatusId)
                ->where('project_id','=',$project->id)
                ->delete();
        }
      

      到这里:

      foreach($twitterStatusIds as $twitterStatusId) 
        {
            ProjectTwitterStatus::where('twitter_status_id', '=', $twitterStatusId)
                ->where('project_id','=',$project->id)
                ->first()
                ->delete();
        }
      

      通过这种方式,模型对象实际上正在加载并且事件有机会触发。当调用 delete() 而不使用 first() Eloquent 时,只需通过 Builder 执行直接 SQL 查询。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-05-08
        • 1970-01-01
        • 1970-01-01
        • 2022-12-04
        • 1970-01-01
        相关资源
        最近更新 更多