Laravel模型的自定义“触摸”功能

时间:2022-10-18 14:35:31

I have a has many relationship in my database and in the child model I have the touches array defined like so

我在我的数据库中有很多关系,在子模型中我有像这样定义的touches数组

 protected $touches = ['parent'];

I am looking to do something similar to what is described in these two questions:

我希望做一些类似于这两个问题所描述的内容:

But with a slight difference, I want the a boolean column on my parent model to be updated when a change occurs in the child model. This works for touches, but I can't figure out how to do it with a custom property instead.

但是稍有不同,我希望在子模型中发生更改时更新父模型上的布尔列。这适用于触摸,但我无法弄清楚如何使用自定义属性来执行此操作。

I have tried this in my parent model with no avail:

我在我的父模型中试过这个但没有用:

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

    static::updating(function ($table) {
        $table->is_finished = true;
    });
}

1 个解决方案

#1


1  

According to this thread it appears that the updating() event only fires in the event that the model was "dirty," i.e. had fields that had changed value, and therefore needed to be updated. So, if no data on the parent model had changed, it could be that your code never gets executed. My guess is that even though the timestamp on the parent is touched, it is exempted from the list of properties that can be considered dirty.

根据这个线程,似乎update()事件仅在模型“脏”的情况下触发,即具有已更改值的字段,因此需要更新。因此,如果父模型上的数据没有更改,则可能是您的代码永远不会被执行。我的猜测是,即使触及父级的时间戳,也可以免除可被视为脏的属性列表。

You might instead just add this code to the child model:

您可能只需将此代码添加到子模型:

// this is whatever property points to the parent model
public function parent() {
    return $this->belongsTo('App\Parent');
}

// this overrides the update() method in the Eloquent\Model class
public function update($attributes = Array, $options = Array) {

    parent::update($attributes, $options);

    $this->parent->is_finished = true;
    $this->parent->save();
}

Haven't tested this, but don't see why it shouldn't work.

没有测试过这个,但不明白为什么它不应该工作。

#1


1  

According to this thread it appears that the updating() event only fires in the event that the model was "dirty," i.e. had fields that had changed value, and therefore needed to be updated. So, if no data on the parent model had changed, it could be that your code never gets executed. My guess is that even though the timestamp on the parent is touched, it is exempted from the list of properties that can be considered dirty.

根据这个线程,似乎update()事件仅在模型“脏”的情况下触发,即具有已更改值的字段,因此需要更新。因此,如果父模型上的数据没有更改,则可能是您的代码永远不会被执行。我的猜测是,即使触及父级的时间戳,也可以免除可被视为脏的属性列表。

You might instead just add this code to the child model:

您可能只需将此代码添加到子模型:

// this is whatever property points to the parent model
public function parent() {
    return $this->belongsTo('App\Parent');
}

// this overrides the update() method in the Eloquent\Model class
public function update($attributes = Array, $options = Array) {

    parent::update($attributes, $options);

    $this->parent->is_finished = true;
    $this->parent->save();
}

Haven't tested this, but don't see why it shouldn't work.

没有测试过这个,但不明白为什么它不应该工作。