$model->attributes and rules()

Hello,

until now I thought model [font="Courier New"]rules()[/font] are used by [font="Courier New"]model->validate()[/font] only, but that does not seem to be the case.

My experiments show that if you assign attributes in bulk by doing [font=“Courier New”]$model->attributes = $_POST[‘model’][/font], it only assigns those attributes that are also specified in the model rules.

My test scenario with a CActiveRecord model:

load existing model (not creating new one), have rules for "user_id" "[font="Courier New"]on=>insert[/font]", make bulk assignment by [font="Courier New"]$model->attributes = $_POST[/font], where post includes user_id in correct form, but the new user_id is not assigned automatically. After that I added an additional "[font="Courier New"]on=>update[/font]" rule for user_id and the assignment worked again.

conclusion:

"[font="Courier New"]on=>insert[/font]" rules apply to [font="Courier New"]->attributes[/font] assignments for newly created models only, "[font="Courier New"]on=>update[/font]" rules apply to loaded models only.

At the same time, [font="Courier New"]validate()[/font] uses those rules for validation before saving it in the database.

Is this correct?

Thank you in advance.

Yes, this seems correct. Also read about scanarios in the wiki: Understanding Yii scenarios

thanks