2013-07-12 91 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

相關問題