How to determine if an ActiveRecord object is 'dirty'?

I am a newbie to ar orm pattern. ActiveRecord is convenient for query relations and create/update on single record, but seems lack of supprot for create/update on relation. I want to do some automatic operations on the relations in the model ( now I put the logic in controller ), but I don’t know how to determine whether an ar object is ‘dirty’, that is, a new object or a loaded object which has been assigned any attribute and need to be saved. Would somebody show me how?

to check use this $model->isNewRecord()

This only checks wether loaded model is a new or existing record. Example of this check is in every _form.php view file generated by gii tool, where it is being used to determine if user should see Add (for new record) or Update (for existing ones) label on submit button.

This AFAIK does not check whether loaded existing model (record) should be saved because it was changed somehow.

So you want to know if any changes have occured to the attributes?

You can maybe make use of afterFind and beforeSave/afterSave to decide if anything needs to be done?

Some example code http://www.yiiframework.com/forum/index.php?/topic/12906-afterdelete-get-model-data-this-caption/page__view__findpost__p__63156 (usering afterDelete rather than beforeSave/afterSave.

In combination with array_diff, it should tell you if something has changed.

e.g. array_diff($this->attributes, $this->_oldValues) this should give a list of all fields that have changed.

Nice idea. This do helps, thanks.