Yii Framework Forum: Virtual Attributes In Model Won't Bind - Yii Framework Forum

Jump to content

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

Virtual Attributes In Model Won't Bind Rate Topic: -----

#1 User is offline   Juniper 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 04-December 12

Posted 04 December 2012 - 07:11 PM

Hi guys,

Whenever I add a virtual attribute field to my model like so:

class User extends CActiveRecord
{
	public $title;


I can't seem to bind the data in my update action in the controller:

public function actionUpdate($id)
	{
		$model = $this->loadModel($id);
		if(isset($_POST['User']))
		{
			$model->attributes = $_POST['User'];

 			if($model->save())
 				$this->redirect(array('view','id' => $model->userId));
		}

		$this->render('update',array(
			'model'=>$model,
		));
	}


Then when I go look at what's in $model->attributes i can't find my $model->title anywhere.
It's in the $_POST['User'], but for some reason it won't bind to my model.
Anyone got any thoughts on how to troubleshoot this?

Thx in advance!
0

#2 User is offline   hehbhehb 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 49
  • Joined: 24-February 12

Posted 04 December 2012 - 08:58 PM

check whether the "title" is defined in the rules of the User model
0

#3 User is offline   Juniper 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 04-December 12

Posted 05 December 2012 - 01:49 PM

Adding a max lenght did the trick, cheers.
Do I always have to define virtual fields in rules to make them show up in my data?
0

#4 User is offline   Da:Sourcerer 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,193
  • Joined: 30-March 11
  • Location:Berlin, Germany

Posted 05 December 2012 - 11:05 PM

This holds true for all attributes: As long as they are not handled by a rule, they cannot be massively assigned. By the way: There's the safe-validator for attributes that do not require any sort of validation ;)
programmer /ˈprəʊgramə/, noun: a device that converts ►coffee into ►code
0

#5 User is offline   rAWTAZ 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 102
  • Joined: 08-January 10

Posted 17 January 2013 - 05:03 PM

View PostDa:Sourcerer, on 05 December 2012 - 11:05 PM, said:

This holds true for all attributes: As long as they are not handled by a rule, they cannot be massively assigned. By the way: There's the safe-validator for attributes that do not require any sort of validation ;)

There is also the 'unsafe'e validator that can be used to make an attribute unsafe.
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