Attributes aren't assigned after change to db table?

I have run into this a few times now, and I am sick of explicitly stating model->attribute = value for each property that I add to the db table.

The problem occurs when I add one or more fields to a database table after the model has been created. I can’t do bulk assignments using model->attributes = array() or model->setAttributes(array()) and expect to have the newly created fields updated in the model. Each time I try this, it’s only the fields that were present when the model was initially created are updated. Which definition within the model is examined when I do a bulk assignment like this?

Thanks,

~thinkt4nk

rules() ???

As Antonio explained, rules are the key for solve this probem.

Yii will automatically assign all properties that are ‘safe’. Since version 1.1 are considered safe all properties on wich there is defined a rule.

So, when you add a new field in the database, you should add the rules for this field (required, integer and so on).

If there are no real rule, you can add a ‘safe’ rule, that will simple mark the attribute as safe:




public function rules()

{

  return array(

     [...]

     array('new_field', 'safe'),

  );

}



Yeah, I guess this was just an issue for me when there were no rules that I wanted to associate to the field. Thanks, guys.