2016-01-17 26 views
0

我有hasToMany關係中的兩個相關模型。第一個是Invoices,第二個是InvoiceItems。爲原型的過程中,我嘗試使用下面的代碼共軛通過update鑑於InvoicesController的兩款車型在actionUpdateInvoicesController的:Yii2更新兩個相關模型不顯示第二個數據

<div class="invoices-update"> 
    <h1><?= Html::encode($this->title) ?></h1> 
    <?= $this->render('_form', [ 
     'model' => $model, 
     'invoiceItems' => $invoiceItems, 
    ]) ?> 
</div> 

... 
use common\models\Invoices; 
use common\models\InvoicesSearch; 
use common\models\InvoiceItems; 
... 

public function actionUpdate($id) 
    { 
     $model = $this->findModel($id); 
     $invoiceItems = new InvoiceItems(); 

     if ($model->load(Yii::$app->request->post()) && $model->save()) { 
      $invoiceItems->invoice_id = $model->id; 
      if ($invoiceItems->load(Yii::$app->request->post()) && $invoiceItems->save()){ 
       return $this->redirect(['view', 'id' => $model->id]); 
      } 
      else{ 
       // return false; 
      } 
     } else { 
      $invoiceItems->invoice_id = $model->id; 
      return $this->render('update', [ 
       'model' => $model, 
       'invoiceItems' => $invoiceItems, 
      ]); 
     } 
    } 

update視圖下最後在_form查看:

<div class="invoices-form"> 
    <?php $form = ActiveForm::begin(); ?> 
    <?= $form->field($model, 'created')->textInput() ?> 
    <?= $form->field($model, 'type')->textInput(['maxlength' => true]) ?> 
    <hr /> 
    <?= $form->field($invoiceItems, 'item_id')->textInput();?> 
    <?= $form->field($invoiceItems, 'unit_id')->textInput();?> 
    <?= $form->field($invoiceItems, 'qty')->textInput();?>  

    <div class="form-group"> 
     <?= Html::submitButton($model->isNewRecord ? Yii::t('app', 'Create') : Yii::t('app', 'Update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?> 
    </div> 

    <?php ActiveForm::end(); ?> 

</div> 

上面的代碼成功保存了invoice_items表中的數據。 但是,更新視圖窗體有InvoiceItems模型的空字段。即item_id,unit_idqty在保存後在更新表單中爲空。

注意:這是初始代碼,即我稍後將能夠添加許多項目到發票,但現在我只是嘗試有一個項目與發票相關。

回答

1

似乎因爲你渲染和空InvoceItmes更新視圖是空的..

$invoiceItems = new InvoiceItems(); 

您呈現在其他章節更新視圖,該欄目發帖值不加載

$model = $this->findModel($id); 
    $invoiceItems = new InvoiceItems(); 

    if ($model->load(Yii::$app->request->post()) && $model->save()) { 
     $invoiceItems->invoice_id = $model->id; 
     if ($invoiceItems->load(Yii::$app->request->post()) && $invoiceItems->save()){ 
      return $this->redirect(['view', 'id' => $model->id]); 
     } 
     else{ 
      // return false; 
     } 
    } else {//****** here the $invoceItems is empty (It has just been created) *** 
     //$invoiceItems->invoice_id = $model->id; 
     $invoceItems= findInvoceItmesModel($model->id); 
     return $this->render('update', [ 
      'model' => $model, 
      'invoiceItems' => $invoiceItems, 
     ]); 
    } 

用於填入$ invoceItems的數據你需要一些像

protected function findInvoiceItemsModel($id) 
{ 
    if (($model = InvociceItems::findOne($id)) !== null) { 
     return $model; 
    } else { 
     throw new NotFoundHttpException('The requested page does not exist.'); 
    } 
} 

顯然t他是一個單一的模型..你必須refactory形式不止一個..

+0

好吧!我需要做些什麼來填充數據? – SaidbakR

+0

當我嘗試'$ invoiceItems = $ model-> getInvoiceItems();'它返回一個錯誤:'調用未知方法:common \ models \ InvoiceItemsQuery :: formName()' – SaidbakR

+0

您需要一個正確的findModel ...我有更新回答... – scaisEdge

相關問題