Display, Update And Save Checkbox List Items From Relations

I have controller where I can create users and add them roles(users, admins, superusers, whatever I want). Role names are saved in a database table called UserRoles(id,name). And then I have another table with connects roles with users- UserRoleAccess(id,user_id,role_id). So I can have multiple roles for one user. I want to add them to create and update section and I have never done that before. I dont know how to do it. This is what I have:

[PHP]

public function actionCreate()


{


    $user=new User('passwordset');


    $person=new Person;


    $access=new UserRoles;


    // Uncomment the following line if AJAX validation is


    //needed


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


    if(isset($_POST['User'], $_POST['Person'], $_POST['UserRoles']))


    {


        print_r($_POST);


        die('');


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


        if($user->save())


        {


            $person->attributes=$_POST['Person'];


            $person->user_id=$user->id;


            if($person->save())


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


        }


    }


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


        'user'=>$user,


        'person'=>$person,


        'access'=>$access,


    ));


}





public function actionUpdate($id)


{


    $model=User::model()->with('person')->findByPk((int)$id);





	// Uncomment the following line if AJAX validation is needed


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





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


	{


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


        if ($model->password || $model->password_repeat)


            $model->scenario = 'passwordset';


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


        if($model->save()) {


            $model->person->attributes=$_POST['Person'];


            $model->person->save();


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


        }


	}





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


		'model'=>$model,


	));


}

[/PHP]

_form.php:

[PHP]

<div class="row">


    <?php echo $form->labelEx($access,'name'); ?>


    <?php echo $form->checkBoxList($access,'name',UserRoleAccess::model()->getAllRoles(),UserRoleAccess::model()->getSelectedRoles());?>


    <?php echo $form->error($access,'name'); ?>


</div>

[/PHP]

UserRoleAccess model:

[PHP]

public function getAllRoles()


{


    $model = UserRoles::model()->findAll();


    return CHtml::listData($model,'id','name');


}





public function getSelectedRoles($id=null)


{


    $selected=[];


    if($id){


        $model=UserRoleAccess::model()->findAllByAttributes(array('role_id'=>$id));


        foreach($model as $mod)


            $selected[]=$mod->id;


    }


    return $selected;


}

[/PHP]

I dont know how I can save it nicely and how to make them checked when editing.

You can check out this wiki: Dynamic Parent and Child Gridviews

So in the Parent-gridview will be the users. In the Child-gridview will be the junction table records - UserRoleAccess - which displays the user’s roles.

You could have a dropdown beneath the Child-gridview where you could select more roles for the user - and then update the Child-gridview via ajax after the new record was created in UserRoleAccess.