Answer the question
In order to leave comments, you need to log in
How and where is the isNewRecord property of the BaseActiveRecord class defined in Yii2?
Good day!
I'm currently learning Yii2 and I've run into this problem.
When generating CRUD through Gii according to the office. The documentation found in the views/country/_form.php view include file that the state of the Create/Update button changes based on the value of the isNewRecord property of the BaseActiveRecord class .
Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary'])
public function getIsNewRecord()
{
return $this->_oldAttributes === null;
}
Answer the question
In order to leave comments, you need to log in
Sergey Protko described everything correctly. The _oldAttributes property is used to implement dirty attributes .
this property is supposedly set immediately after the data is loaded from the database (in oldAttributes and attributes). So, before saving, we can check what we have changed, since only attributes will be subject to changes. Well, for the newly minted model, oldAttributes will always be empty.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question