Answer the question
In order to leave comments, you need to log in
How to properly work with beforeValidate and isNewRecord in yii2?
Hello. Time after time I run into the same problem.
For new entries in the database, I need to fill in some fields, such as the update date. To achieve my goal, I use a public function beforeValidate() and an if ($this->isNewRecord) which should literally fire "do before validation" and "if the record is new". But in fact $this->dtUpdate=time(); is always performed. It doesn't matter if the record is new or not.
If, for example, I display data in a GridView with the ability to filter, then the dtUpdate field for filters will always be filled with the current date..
Answer the question
In order to leave comments, you need to log in
public function beforeSave($insert)
{
if ($insert){ ваш код }
return parent::beforeSave($insert); // TODO: Change the autogenerated stub
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question