Yii Framework Forum: Attributes aren't assigned after change to db table? - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Attributes aren't assigned after change to db table? Rate Topic: -----

#1 User is offline   thinkt4nk 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 36
  • Joined: 19-September 10

  Posted 02 November 2010 - 09:19 AM

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
0

#2 User is offline   Antonio Ramirez 

  • Elite Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 1,450
  • Joined: 04-October 10

Posted 02 November 2010 - 09:23 AM

rules() ???
┬┐How long would it take for you to understand that you own nothing in this world?

www.ramirezcobos.com
www.2amigos.us
www.github.com/tonydspaniard
www.github.com/2amigos


Posted Image
0

#3 User is offline   zaccaria 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 2,232
  • Joined: 04-October 09
  • Location:Moscow

Posted 02 November 2010 - 09:59 AM

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'),
  );
}

0

#4 User is offline   thinkt4nk 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 36
  • Joined: 19-September 10

Posted 02 November 2010 - 10:02 AM

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.


View Postzaccaria, on 02 November 2010 - 09:59 AM, said:

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'),
  );
}


0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users