ACtiveRecord support update multimodels
Code First ^&^
{Controller}
public function actionUpdateAll() { for ($i=1;$i<7;$i++)//generate seven models with name model_$i ${'model_'.$i} = $this->loadModel($i); // Uncomment the following line if AJAX validation is needed // $this->performAjaxValidation($model); if(isset($_POST['PPreviewMap1'])) { for ($i=1;$i<7;$i++){ ${'model_'.$i}->attributes=$_POST['PPreviewMap'.$i]; ${'model_'.$i}->save(); } $this->redirect(array('updateAll')); } for ($i=1;$i<7;$i++)//packaging as an array for passed usage $arrPass['model_'.$i] = ${'model_'.$i}; $this->render('updateAll',array( 'arrPass'=>$arrPass, )); }
{View : updateAll}
renderPartial('_formAll', $arrPass); ?>
{View : _formAll}
labelEx(${'model_'.$i},'map_id'); ?>
textField(${'model_'.$i},'map_id',array('size'=>10,'maxlength'=>10, 'name'=>'PPreviewMap'.$i.'[map_id]')); ?>
error(${'model_'.$i},'map_id'); ?>
Here is my point : >> 'name'=>'PPreviewMap'.$i.'[map_id]', the name will be PPreviewMap1[map_id], PPreviewMap2[map_id], PPreviewMap3[map_id], it's easy for get and update in controller action.
Leave Comment