Add Field To User Form And Save It To Db

Hi I am new to Yii. I have a form in which i add (or update) a new user, and i added a field (list - select box) called ‘RBAC’ to add the new user to a specified role. How can i get this value and store it in DB while also creating the new user?

I only see this on the create/update actions:


$model=new User;


		// Uncomment the following line if AJAX validation is needed

		// $this->performAjaxValidation($model);


		if(isset($_POST['User']))

		{

			$model->attributes=$_POST['User'];

			if($model->save())

				$this->redirect(array('view','id'=>$model->id));

		}


		$this->render('create',array(

			'model'=>$model,

		));

Did you use gii to generate the model, controller, views?

The best is to regenerate the user model / crud from db with gii.

Otherwise you have to add the field to

  • User model methods: rules(),attributeLabels(), search()

  • gridview in admin.php and _form.php for create/update.

Yes i did.

If i regenerate it with gii, will it overwrite everything (including custom code)?

Just add it in your model

inside


public function rules()

	{




And if you dont want to validation , then just add


public $RBAC;

inside the model class file.

Great, thanks

welcome :)