added attributes to model won't save

Hi,

I added date_received and contributor to the model’s rules and attributeLabels, so far it was able to read it from the view, but it won’t save changes to the two added attributes. What did I missed?


public function rules()

	{

		// NOTE: you should only define rules for those attributes that

		// will receive user inputs.

		return array(

			array('title, category, description, keyword','required'),

			array('title', 'length', 'max'=>128),

			array('keyword', 'match', 'pattern'=>'/^[\w\s,]+$/', 'message'=>'Tags can only contain word characters.'),

                        array('keyword', 'normalizeKeywords'),

			// The following rule is used by search().

			// Please remove those attributes that should not be searched.

			array('title, date_received, category, description, keyword, create_time, update_time, contributor', 'safe', 'on'=>'search'),

		);

	}


	public function attributeLabels()

	{

		return array(

			'id' => 'ID',

			'date_received' => 'Date Received',

			'title' => 'Title',

			'category' => 'Category',

			'description' => 'Description',

			'keyword' => 'Keyword',

			'create_time' => 'Create Time',

			'update_time' => 'Update Time',			

			'author_id' => 'Author',

			'contributor' => 'Contributor',

		);

	}

You need to put date_received under a validation rule. Putting it only under search will not matter to Yii when saving (that’s only for the search method). Hope that helps.

Hi,

Unfortunately, it was already included in the rules(), I placed it under the last array:




array('title, date_received, category, description, keyword, create_time, update_time, contributor', 'safe', 'on'=>'search'



That rule does apply only on search you have to use different validation rule when saving. Though on second thought those attributes don’t sound as user inputs so the problem might be in your saving method…

If that’s the case, which method should I be looking for?

The only thing I have that modifies the model is beforeSave()? Are there other things to look for?

Dear CorbeeResearch

Follwoing are my suggestions.I hope those are helpful.

Add a new rule.




array('date_received,contributor', 'safe', 'on'=>'insert'),//insert and update are default scenarios available inside the AR record.



or modify the last rule




array('title, date_received, category, description, keyword, create_time, update_time, contributor', 'safe', 'on'=>array('search','insert')),



Regards.

You need to add some rules for ‘date_received’ and ‘contributer’.

If you use them in the user input form, then something like this:




public function rules()

{

	return array(

		array('title, category, description, keyword, date_received, contributer','required'),

		array('title', 'length', 'max'=>128),

		array('keyword', 'match', 'pattern'=>'/^[\w\s,]+$/', 'message'=>'Tags can only contain word characters.'),

                array('keyword', 'normalizeKeywords'),

		array('date_received', 'date', 'format'=>'yyyy/MM/dd'),

		array('contributer', 'length', 'max'=>64),

		// The following rule is used by search().

		// Please remove those attributes that should not be searched.

		array('title, date_received, category, description, keyword, create_time, update_time, contributor', 'safe', 'on'=>'search'),

	);

}



Or if you handle the values of them only by the program, without any user input, then you can declare them to be ‘safe’, just as the other people suggested.




public function rules()

{

	return array(

		array('title, category, description, keyword','required'),

		array('title', 'length', 'max'=>128),

		array('keyword', 'match', 'pattern'=>'/^[\w\s,]+$/', 'message'=>'Tags can only contain word characters.'),

                array('keyword', 'normalizeKeywords'),

		array('date_received, contributer', 'safe'),

		// The following rule is used by search().

		// Please remove those attributes that should not be searched.

		array('title, date_received, category, description, keyword, create_time, update_time, contributor', 'safe', 'on'=>'search'),

	);

}



The last rule is only applied when you create the model instance with ‘search’ scenario.

You should not declare the last rule also applicable to ‘insert’ scenario. If you do so, then all the user inputs are saved to db without any checking when the user is in “create” page.