How To Update Multiple Checkboxes In Another Model

Hi everybody and thanks for reading .

I have a Create form where i create meeting’s. Each meeting has some participants that are saved on bridge table.

This is the code i use on the Meetings controler to insert a new meeting and the participants to the meeting ;


public function actionCreate()

	{

		$model=new Meeting;

               

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

		{

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

                        

                      

			if($model->save())

                            //upload the partecipants

                                $partecipants = $_POST['Meeting']['partecipants'];    

                          if(!empty($partecipants))  

                            {

                            

                                foreach($partecipants as $partecipant){

                                        $bridge=new BridgeWg();

                                        $bridge->id_wg=$model->id_wg;

                                        $bridge->id_user_registry=$partecipant;

                                        $bridge->save(false);

                                }

                            }

                                                     

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

		}


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

			'model'=>$model,

		));

	}

in the view i have the following code


 <?php echo $form->checkBoxListInlineRow($model, 'partecipants', CHtml::listData(UserRegistry::model()->findAll(), 'id_user_registry', 'firstname')); ?>

"partecipants" is a filed not present in the db so i defined it as public $participants in the model Meetings

Anybody has an idea how i can load the participants on the update page and have all the boxes checked of the participants i inserted during the creation of the Meeting.

Let me know if anybody has an idea or a link i could reffer to .

Thanks in advance

Dear Friend

Would you please check the following?

In your model override the afterFind method.

Model(Meeting.php)




public function afterFind()

{

$this->partecipants=Yii::app()->db->createCommand()->select("id_user_registry")->from('bridge_table')->where('id_wg='.$this->id_wg)->queryColumn();

return parent::afterFind();

}



This will bring all the members id who attended the paricular meeting in array format.

This will nicely check the checkboxes during update.

Also put your bridge table name correctly in the code.

Regards.

Thanks friend , i added the above code to my controler , whats next , do i have to add somethingh to the view (_form)?

Dear Friend

If you have added the $partecipants as virtual property to the model, that would be enough.

Now if we update the record, then checkboxes would be checked according to database values.