Help With Checkbox Updating

Hello.

(Sorry for my English)

I have a model where you can select some tasks of a list and for each task a company.

What I’ve done is a CGridView with a CheckBox column and a DropDown. This is the _form:


                    $criteria = new CDbCriteria();

                    //Queremos mostrar las Acciones Promocionadas de este año

                    $criteria->condition = "ano=$añoActual";

                    $datos=new CActiveDataProvider('AccionPromocionada', array('criteria'=>$criteria));

                    

                    $this->widget('zii.widgets.grid.CGridView', array(

                        'dataProvider'=>$datos,

                        'columns'=>array(

                            array('class'=>'CCheckBoxColumn',

                                'header'=>'',

                                'checked'=>'',

                                'selectableRows'=>8,

                                'id'=>'accionPromocionada',

                                ),

                            array('header'=>'Acciones promocionadas',

                                'name'=>'descripcion',

                                'value'=>'$data->descripcion'),

                            //Agregamos la columna con todas las empresas homologadas. ->DropDown

                            array('name' =>'Empresa Seleccionada',

                                  'type' => 'raw',

                                  'value'=>function($data) {

                                        return '<div id="homologada[]">'.CHtml::dropDownList("empresaHomologada[]","empresaHomologada",CHtml::listData(EmpresaHomologada::model()->findAll(),"id", "nombre")).'</div>';

                                   },

                    ))));



In the Create action there is no problem: I can get the checked tasks and selected companys; but in the Update, I don’t know how to check the selected items and the dropdown.

If I could update with the actual model it would be great, but I think I could change to a CheckBoxList or ActiveCheckboxList (I don’t know the difference).

Thanks in advance.

[font="Arial"][size="2"]Please use this when update a record [/size][/font]

[font="Arial"] [/font]

[font=Arial][size=2]


echo CHtml::dropDownList('designation_id', 'designation_id', CHtml::listData(VenueDesignation::model()->findAll("status='1'"),'id','designation_name'), array('options' => array($resultSet->designation_id => array('selected' => true),'class'=>'col_165')));

[/size][/font]

[font=Arial][size=2]

[/size][/font]

Thanks for your answer. Can you explain what $resultSet is? I suppose it’s a $_POST value.

I found a solution for the first question: select the items in the update.


                <?php

                    $arraySolicitadas="";


                    //Queremos mostrar las Acciones Promocionadas de este año

                    $datos=new CActiveDataProvider('AccionPromocionada', array('criteria'=>array('condition'=>'ano='.$añoActual)));

                            

                    //Si estamos haciendo una Actualizacion, obtenemos una condición para marcar las seleccionadas,

                    //accediendo a la tabal de AccionesSolicitadas.

                    if (!$model->isNewRecord){

                            $criteria = new CDbCriteria();

                            $criteria->condition = 'solicitudId='.$model->id;

                            $accionesSolicitadas = AccionSolicitada::model()->findAll ($criteria);


                            //Creamos un array para poder comparar en el CGridView

                            $arraySolicitadas = CHtml::listData( $accionesSolicitadas, 'id', 'accionPromocionadaId');

                    }

                    

                    $this->widget('zii.widgets.grid.CGridView', array(

                        'dataProvider'=>$datos,

                        'columns'=>array(

                            array('class'=>'CCheckBoxColumn',

                                'header'=>'',

                                'checked'=>function($data) use($arraySolicitadas) {

                                                                return in_array($data->id, (array)$arraySolicitadas);},

                                'selectableRows'=>8,

                                'id'=>'accionPromocionada',

                                ),

                            array('header'=>'Acciones promocionadas',

                                'name'=>'descripcion',

                                'value'=>'$data->descripcion'),

                            //Agregamos la columna con todas las empresas homologadas.

                            array('name' =>'Empresa Seleccionada',

                                  'type' => 'raw',

                                  'value'=>function($data) {

                                        return '<div id="homologada[]">'.CHtml::dropDownList("empresaHomologada[]","empresaHomologada",CHtml::listData(EmpresaHomologada::model()->findAll(),"id", "nombre"), array('prompt'=>'')).'</div>';

                                   },

                    ))));

                ?> <!-- Fin listado Acciones Promocionadas -->



Now I’m going to prove the second point: select the correct company in the DropDown List.

This is what I’m doing:




                    $empresasSeleccionadas=AccionSolicitada::model()->findAll(

                        array('condition'=>'solicitudId=:solicitudId',

                              'params'=>array('solicitudId'=>$model->id),

                              'order'=>'accionPromocionadaId ASC'));

                    .

                    .

                    .


                            array('name' =>'Empresa Seleccionada',

                                  'type' => 'raw',

                                  'value'=>function($data)  use($empresasSeleccionadas)  {

                                        return '<div id="homologada[]">'

                                             .CHtml::dropDownList('empresaHomologada[]','empresaHomologada', CHtml::listData(EmpresaHomologada::model()->findAll(),'id', 'nombre'),

                                                       array('prompt'=>''),

                                                       array('options' => array($empresasSeleccionadas->empresaHomologadaId => array('selected' => true))))

                                               .'</div>';

                                   },

                    )



The output I get is:


Trying to get property of non-object 



The output of the $empresasSelecionadas var_dump:




array (size=1)

  0 => 

    object(AccionSolicitada)[417]

      private '_new' (CActiveRecord) => boolean false

      private '_attributes' (CActiveRecord) => 

        array (size=6)

          'id' => string '3' (length=1)

          'ano' => string '2014' (length=4)

          'esEmpresaSeleccionada' => string '1' (length=1)

          'solicitudId' => string '2' (length=1)

          'accionPromocionadaId' => string '3' (length=1)

          'empresaHomologadaId' => string '1' (length=1)

      private '_related' (CActiveRecord) => 

        array (size=0)

          empty

      private '_c' (CActiveRecord) => null

      private '_pk' (CActiveRecord) => string '3' (length=1)

      private '_alias' (CActiveRecord) => string 't' (length=1)

      private '_errors' (CModel) => 

        array (size=0)

          empty

      private '_validators' (CModel) => null

      private '_scenario' (CModel) => string 'update' (length=6)

      private '_e' (CComponent) => null

      private '_m' (CComponent) => null



One solution I think could solve this is using AfterRender o AfterAction: after generate the view code with the DropDown, set the selected items where needed using javascript.

Another solution I could do is changing CGridView and using other different widget or more flexible construction.

Which I finally want is a list of selectable actions and for each action you can pick into a list of options. My problem is with the Update. Here I show a screen.

I was thinking about YiiBooster and its TbEditableColumn using, but I couldn’t see how obtain the selected values in the update, so finally I wrote the code.