yii2 钩子函数

插入时间

public function beforeSave($insert)
{
    $this->created_at=time();
    return parent::beforeSave($insert); // TODO: Change the autogenerated stub
}

 钩子函数注意事项

public function beforeSave($insert)

若是添加$insert为true
更新时
旧数据读取
$this->oldAttributes
新数据读取
$this->attributes['src']
$this->src

public function afterSave($insert, $changedAttributes)

若是添加$insert为true,否则为false

更新

旧数据

$changedAttributes['src']

新数据读取

$this->oldAttributes
$this->attributes['src']
$this->src

新增

$changedAttributes为空,其他都为新增数据。

 public function afterDelete()

删除后读取数据

$this->attributes['src']
$this->src


原文地址:https://www.cnblogs.com/huay/p/11460013.html