2013-07-12 58 views
1

在_form.php文件中我有两个不同型号的文本框,我对create控制器进行了更改,以便为两个型号插入记录,但问题出在update控制器中,我如何加载第二个模型的文本框中的第二个模型的数据?
只有与第一个模型相关的文本框才会被填充。如何在更新控制器中加载多个模型

+0

那你尝试做解决这个问题呢?请显示你的代码。可能会有所帮助:http://www.yiiframework.com/wiki/291/update-two-models-with-one-view/ – Sergey

回答

1

只需在控制器中将两个模型都渲染到update视图。

$this->render('update', array(
    'model1' => $model1, 
    'model2' => $model2, 
)); 

,并在您_form.php通话textboxs这样

<?php echo $form->labelEx($model1, ‘data1’); ?> 
<?php echo $form->textField($model1, ‘data1’, array(‘size’ => 60, ‘maxlength’ => 250)); ?> 

<?php echo $form->labelEx($model2, ‘data2’); ?> 
<?php echo $form->textField($model2, ‘data2’, array(‘size’ => 60, ‘maxlength’ => 250)); ?> 

希望这会有所帮助。

编辑

因为你可能会使用相同的_form.php这个页面create视图,您需要创建另一个_form.php这个文件,说_formUpdate.php页[_form.php这个副本]和从update.php呼叫呈现_formUpdate.php而不是_form.php这个和上面所做的更改

+0

我试图使用上述建议,但数据未填入地址文本框 –

+0

它现在的作品,谢谢你 –

+0

很高兴知道这一点。 :)如果确实有效,请将其标记为答案,谢谢 – zzlalani

0

_form

<div class="form"> 

<?php $form=$this->beginWidget('CActiveForm', array(
    'id'=>'tabel1-form', 
    'enableAjaxValidation'=>false, 
)); ?> 

    <p class="note">Fields with <span class="required">*</span> are required.</p> 

    <?php echo $form->errorSummary($model); ?> 

    <div class="row"> 
     <?php echo $form->labelEx($model,'name'); ?> 
     <?php echo $form->textField($model,'name',array('size'=>44,'maxlength'=>44)); ?> 
     <?php echo $form->error($model,'name'); ?> 
    </div> 
    <div class="row"> 
     <?php echo $form->labelEx(Table2::model(),'address'); ?> 
     <?php echo $form->textField(Table2::model(),'address',array('size'=>44,'maxlength'=>44)); ?> 
     <?php echo $form->error(Table2::model(),'address'); ?> 
    </div> 

    <div class="row buttons"> 
     <?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?> 
    </div> 

<?php $this->endWidget(); ?> 

</div><!-- form --> 

update.php

<?php 
$this->breadcrumbs=array(
    'Tabel1s'=>array('index'), 
    $model->name=>array('view','id'=>$model->id), 
    'Update', 
); 

$this->menu=array(
    array('label'=>'List Tabel1', 'url'=>array('index')), 
    array('label'=>'Create Tabel1', 'url'=>array('create')), 
    array('label'=>'View Tabel1', 'url'=>array('view', 'id'=>$model->id)), 
    array('label'=>'Manage Tabel1', 'url'=>array('admin')), 
); 
?> 

<h1>Update Tabel1 <?php echo $model->id; ?></h1> 

<?php echo $this->renderPartial('_formUpdate', array('model'=>$model,'model2'=>$model2)); ?> 

Tabel1Controller.php

public function actionCreate() 
    { 
     $model=new Tabel1; 
    $model2=new Table2; 
     // Uncomment the following line if AJAX validation is needed 
     // $this->performAjaxValidation($model); 

     if(isset($_POST['Tabel1'])&&isset($_POST['Table2'])) 
     { 
      $model->attributes=$_POST['Tabel1']; 
         $model2->attributes=$_POST['Table2']; 
      if($model->save()&&$model2->save()) 
       $this->redirect(array('view','id'=>$model->id)); 
     } 

     $this->render('create',array(
      'model'=>$model, 
        'model2'=>$model2 
     )); 
    } 

    /** 
    * Updates a particular model. 
    * If update is successful, the browser will be redirected to the 'view' page. 
    * @param integer $id the ID of the model to be updated 
    */ 
    public function actionUpdate($id) 
    { 
     $model=$this->loadModel($id); 
    $model2=$this->loadModel2($id); 
     // Uncomment the following line if AJAX validation is needed 
     // $this->performAjaxValidation($model); 
//echo $model2->address; 
     if(isset($_POST['Tabel1'])&&isset($_POST['Table2'])) 
     { 
      $model->attributes=$_POST['Tabel1']; 
         $model2->attributes=$_POST['Table2']; 
      if($model->save()&&$model2->save()) 
       $this->redirect(array('view','id'=>$model->id)); 
     } 

     $this->render('update',array(
      'model'=>$model, 
        'model2'=>$model2 
     )); 
    } 
public function loadModel2($id) 
    { 
     $model=Table2::model()->findByPk(1); 
     if($model===null) 
      throw new CHttpException(404,'The requested page does not exist.'); 
     return $model; 
    } 
+0

我试着用什么zzlalani建议,但数据未填入文本框 –

+0

请检查我的意见 – zzlalani

+1

我的意思是检查你的答案我的编辑。 – zzlalani

相关问题