Populate checkBox

Hi,

I have in a _form, a checkBoxList that I populate with data belonging to the model (in fact more complicate, becauseit is from a MANY_MANY relationship).


     <h2>My options</h2>

     <?php  

$model->options = array(array('id'=>1,'name'=>'blup'),array('id'=>2,'name'=>'blup'));?>

     <div id="panel-options">

        <?php  echo CHtml::activeCheckBoxList(

                $model,

                'options',

                CHtml::listData($model->options,'id','name'),

            

            array(

                'template' => '<div class="check-option">{input} {label}</div>',

                'separator' => '',

               )

            );

        ?>

     </div>

My _form should work as well as for Create and for Update as it is intended in Yii.

I’m unable to have (in case of Update action) all the checkboxes checked.

[b]Then correct behavior would be:

Retreiving all possible options, then checked the one which belong to the current model.[/b]

Is there a way to have checked boxes ?

Thanks from a newbie…

Ok, it works.

Good way of doing is

-1- Defining in the controller, in loadModel, something which populates the attributes given in the relation.

For me it was


 'options' =>array(self::MANY_MANY,'AdminOption','tbl_voiture_option(voiture_id,option_id)'),

Then in my model it exists the options attribute.

I have to populate it.

Then in the controller/loadModel:

a/ You should get, for the current model, the id of the options.

Doing this by a simple query.


          $criteria = new CDbCriteria;

          $criteria->condition = 'car_id=:car_id';

          $criteria->select = 'option_id';

          $criteria->params=array(':car_id'=>$_GET['id']);

          

          $optionsQuery = AdminCarOption::model()->findAll($criteria);



b/ you should provide with this result an array, instead of objects. This array contains all options Id used by the current model (car in fact. A car as Many options and options as many cars…)

Make a foreach throughout $optionsQuery to get the id of the options.

c/

You should set ‘options’ attribute with the built array

$this->options = the array of options Id

d/in the _form (in may cars _form)


     <?php  

     <div id="panel-options">

        <?php  echo CHtml::activeCheckBoxList(

                $model, // The car model

                'options', // Contains the array of 'options' Id, used by this car model

                 CHtml::listdata(AdminOption::model()->findAll(),'id','nom'), // To get ALL the possible options

            

            array(

                'template' => '<div class="check-option">{input} {label}</div>',

                'separator' => '',

               )

            );

        ?>

     </div>

Then the widget will be able to spot which options hve to be checked and the one which should not.

Works perfect.

Sorry for my poor English …