I want to check if some attributes in my model are changed beforeSave()to match the data stored in the database.
beforeSave()
Is there any best practice to check if the model has changed?
My goal is si to make a story. If any attribute changes, I save a copy in the table model_history.
model_history
You can do this with afterSave().
afterSave()
public function afterSave($insert, $changedAttributes) { parent::afterSave($insert, $changedAttributes); if(!$insert) { // your code here like $changedAttributes['myField']; } }
$changedAttributes saves attribute values that have been changed.
$changedAttributes
You need to check for dirty attributes
yii\db\ActiveRecord::getDirtyAttributes(), , .
yii\db\ActiveRecord::getDirtyAttributes()
http://www.yiiframework.com/doc-2.0/guide-db-active-record.html#dirty-attributes
Source: https://habr.com/ru/post/1670971/More articles:Image headers giving poor results - pythonHow to compose classes in Scala? - scalaКоличество повторов в неупорядоченной карте - c++https://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1670969/angular2-adding-routerlink-parameters-to-chosen-table-value&usg=ALkJrhjiZeWBxmPbmkr5eqh0_gZ_jjvs2wSchedule algorithm, finding all non-overlapping intervals of a given length - optimizationRound time at X hours in R? - dateChange row row after applying transformations - ruby | fooobar.comR: how do you make row sums based on grouping from other variables? - rCharacter data not allowed by content model - XSD - xmlCalling a spider therapist from Django - pythonAll Articles