Yii Framework Forum: Massive Assignment Non-Database Column Fields In Model - Yii Framework Forum

Jump to content

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

Massive Assignment Non-Database Column Fields In Model Rate Topic: -----

#1 User is offline   IainG 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 15
  • Joined: 14-December 11

Posted 25 January 2013 - 11:38 AM

I have a CActiveRecord model, Job which has a public property $completionChoice.

It doesn't relate to a databasefield, and I don't have getter or setter methods for it. I use it as a flag for some processing during beforeSave()

What I'd like to be able to do is massively assign from the form, so have $_POST['completionChoice'] automatically get assigned to it when I do $job->attributes() = $_POST['Job'], however I can't get it working.

I've tried setting $completionChoice as safe, and I've tried overriding CActiveRecord::attributeNames() to add it, but it doesn't get assigned.

How do I do this?

Thanks

Iain
0

#2 User is offline   seenivasan 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 620
  • Joined: 17-June 12
  • Location:Chennai,TamilNadu,India.

Posted 25 January 2013 - 12:08 PM

Dear Friend

I have a Model User.

I have a public property.
public $native="India";


I have made it safe during massive assignement.
array('native', 'safe'),


This is the controller logic.
public function actionCreate()
	{
		$model=new User;

		if(isset($_POST['User']))
		{  
                        $_POST['User']['native']="United States";
			$model->attributes=$_POST['User'];
			if($model->validate())
			    echo $model->native;

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

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


Now every time I submitting the form "United States" getting echoed.

Regards.
1

#3 User is offline   IainG 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 15
  • Joined: 14-December 11

Posted 25 January 2013 - 12:45 PM

View Postseenivasan, on 25 January 2013 - 12:08 PM, said:

Dear Friend

I have a Model User.

I have a public property.
public $native="India";


I have made it safe during massive assignement.
array('native', 'safe'),


This is the controller logic.
public function actionCreate()
	{
		$model=new User;

		if(isset($_POST['User']))
		{  
                        $_POST['User']['native']="United States";
			$model->attributes=$_POST['User'];
			if($model->validate())
			    echo $model->native;

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

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


Now every time I submitting the form "United States" getting echoed.

Regards.


You're quite right, it does work. The problem was with an error in another validation rule. I have fixed it now.

Thank you!

Iain
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