Yii2 afterSave()中的$changedAttributes是否与$this相同->;getDirtyAttributes()?

Yii2 afterSave()中的$changedAttributes是否与$this相同->;getDirtyAttributes()?,yii2,yii2-model,Yii2,Yii2 Model,从上面的代码中,我了解到afterSave()方法中的变量$changedAttributes与$this->getDirtyAttributes()相同,对吗?否。getDirtyAttributes()在保存对象后返回状态,而$changedAttributes在保存前返回状态$changedAttributes还仅包含在save()或update()调用期间保存的属性,而不是所有已更改的属性。因此,如果模型有两个字段:id和name,以及: 如果更改这两个字段并调用save(),则在aft

从上面的代码中,我了解到
afterSave()
方法中的变量
$changedAttributes
$this->getDirtyAttributes()
相同,对吗?

否。
getDirtyAttributes()
在保存对象后返回状态,而
$changedAttributes
在保存前返回状态
$changedAttributes
还仅包含在
save()
update()
调用期间保存的属性,而不是所有已更改的属性。因此,如果模型有两个字段:
id
name
,以及:

  • 如果更改这两个字段并调用
    save()
    ,则在
    afterSave()
    中,$this->getDirtyAttributes()
    将返回空数组(因为对象中没有未保存的更改),而
    $changedAttributes
    将包含两个具有旧值的属性(因为两个属性都已保存)
  • 如果更改这两个字段并调用
    save(true,['id'])
    ,则
    $this->getDirtyAttributes()
    将返回值为
    name
    的数组(因为该属性已更改,但尚未保存),
    $changedAttributes
    将包含值为
    id
    的数组(因为该属性已更新)

  • 有关详细信息,请参阅实现。

    否。
    getDirtyAttributes()
    在保存对象后返回状态,而
    $changedAttributes
    在保存前返回状态
    $changedAttributes
    还仅包含在
    save()
    update()
    调用期间保存的属性,而不是所有已更改的属性。因此,如果模型有两个字段:
    id
    name
    ,以及:

  • 如果更改这两个字段并调用
    save()
    ,则在
    afterSave()
    中,$this->getDirtyAttributes()
    将返回空数组(因为对象中没有未保存的更改),而
    $changedAttributes
    将包含两个具有旧值的属性(因为两个属性都已保存)
  • 如果更改这两个字段并调用
    save(true,['id'])
    ,则
    $this->getDirtyAttributes()
    将返回值为
    name
    的数组(因为该属性已更改,但尚未保存),
    $changedAttributes
    将包含值为
    id
    的数组(因为该属性已更新)
  • 有关更多信息,请参考实施

    public function afterSave($insert, $changedAttributes)
    {
        parent::afterSave($insert, $changedAttributes);
    
        // code for after save
    }