2011-03-24 06:46:18 0 评论 Yii 1.0 Boy.Lee

ACtiveRecord 支持多Model

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'); ?>

重点 'name'=>'PPreviewMap'.$i.'[map_id]', 形成的下标将为 PPreviewMap1[map_id], PPreviewMap2[map_id], PPreviewMap3[map_id], 这对在controller的action里进行更新和获取值的操作是非常方便的。