Form for many models of the same class - problem

Hello.

I’ve made CGridView inside CAvtiveForm. CGridView uses my $dataProvider which is array of CActiveRecord (many rows od the same model). It works fine, I can edit data in rows, but problem is that, when I submit - my form in $_POST data send only last row. I am not supprised that it works like that since every input in my grid has the same name from the same model and they overwrites themselves in POST table and only last one is send.

I’ve tried to solve it but I can’t get good idea how to change names of my imputs (for eample add row number into name) or to make my form to treat that as Array of models.

Below sample of my code from view:




<?php $form=$this->beginWidget('CActiveForm', array(

'id'=>'form-id',

'enableAjaxValidation'=>false,

)); ?>


<?php $this->widget('zii.widgets.grid.CGridView',

array( 'id'=>'grid-id',

'dataProvider'=>$dataProvider, // or $model->search()

'filter'=>$model,

'columns'=>array(

array( 'name'=>'id',

'class'=>'CDataColumn',

'type'=>'raw',

'value'=>'CHtml::activeTextField($data, \'id\', array(\'readonly\'=>\'readonly\'))',

),

array( 'name'=>'item_name',

'class'=>'CDataColumn',

'type'=>'raw',

'value'=>'CHtml::activeTextField($data, \'item_name\')',

),

/*

rest of columns was here

*/

array( 'class'=>'CButtonColumn',

),

),

)); 

echo CHtml::submitButton('Update');

$this->endWidget();

?>




Action from controller:




public function actionView()

{

$dataProvider = new CActiveDataProvider('Item',

array(

'pagination'=>array(

'pageSize'=>10,

),

)

);


$model = new Item('search');

$model->unsetAttributes();

if (isset($_GET['Item']))

$model->attributes=$_GET['Item'];


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

'model'=>$model, 'dataProvider'=>$dataProvider,

));

}




There is no saving data yet, becouse it doesnt send what i need.

I’ll be greatfull for good ideas how to make it work (-> send all items from grid in POST).

Hello again.

I see some views of my topic, but no reply for now. For the time I managed to send my all models in POST by adding row number to imput name:

It looks like that:




<?php $form=$this->beginWidget('CActiveForm', array(

'id'=>'form-id',

'enableAjaxValidation'=>false,

)); ?>


<?php $this->widget('zii.widgets.grid.CGridView',

array( 'id'=>'grid-id',

'dataProvider'=>$dataProvider, // or $model->search()

'filter'=>$model,

'columns'=>array(

array( 'name'=>'id',

'class'=>'CDataColumn',

'type'=>'raw',

'value'=>'CHtml::activeTextField($data, \'id\', array(\'readonly\'=>\'readonly\', \'name\'=>\'Item_\'.$row.\'[id]\'))',

),

array( 'name'=>'item_name',

'class'=>'CDataColumn',

'type'=>'raw',

'value'=>'CHtml::activeTextField($data, \'item_name\', array(\'name\'=>\'Item_\'.$row.\'[item_name]\'))',

),

/*

rest of columns was here

*/

array( 'class'=>'CButtonColumn',

),

),

)); 

echo CHtml::submitButton('Update');

$this->endWidget();

?>




where $row is row number from CGridView.

This method somehow solves the problem, but I am not satisfied with that. In $_POST table it is now send as "Item_0", "Item_1", "Item_2", ect…

If anyone have better idea - I am opened for suggestions.

this post might help you