B
B
BloodVIRUS2021-12-17 10:56:05
Yii
BloodVIRUS, 2021-12-17 10:56:05

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

1 answer(s)
D
Dmitry Bay, 2021-12-17
@BloodVIRUS

public function beforeSave($insert)
    {
         if ($insert){ ваш код }
        return parent::beforeSave($insert); // TODO: Change the autogenerated stub
    }

But it is better to translate these behaviors into Behaviours.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question