Detecting ActiveRecord variable changes?

Just curious to see if there is an alternative way for detecting to see if the user has changed the values in a model before it saves.

Currently right before the $model->save() happens, I have a block of code that goes out to the database and grabs the same record to do a before and after comparison.

        $criteria = new CDbCriteria;



      	$oldModel = entry::model()->find($criteria);



      	  $model->determineState($oldModel); // where we determine if anything changed



Assuming the model is derived from CActiveRecord:

CActiveRecord has a property tableSchema which is of type CDbTableSchema.

In CDbTableSchema is a property columnNames.

Add a private property $_dataChanged to your model which is set to true (in __set()) when a value changes.

Override the magic method __set():

public funcion __set($prop)







To reset the $_dataChanged property:

public function afterSave()




I need to look for specific variables that have changed, but I think this will work perfectly. Thanks for your help.

you can record the old values on afterFind(),

and add determineState() function in your model, compare and get the result~

I could also do this, thanks. :lol:

Thanks for your answer! It works for me too.

But take a look to the magic method __set specification:

Without second parameter $value I get the PHP error.

public void __set ( string $name , mixed $value )