Answer the question
In order to leave comments, you need to log in
How to add properties to model after save in Yii2?
Hello. I'm trying to add the current value of the date to the model when Create , but for some reason I always get null in the database.
public function afterSave($insert, $changedAttributes)
{
parent::afterSave($insert, $changedAttributes);
$this->date = new \DateTime();
}
Answer the question
In order to leave comments, you need to log in
Good evening.
Use the behavior and everything will be fine.
Add the following code to the models:
use yii\behaviors\TimestampBehavior;
public function behaviors()
{
return [
TimestampBehavior::className()
];
}
First, not after (after saving), but before.
Secondly, better www.yiiframework.com/doc-2.0/yii-behaviors-timesta...
And for other fields (for example, user) www.yiiframework.com/doc-2.0/yii-behaviors-attribu...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question