2016-01-17 41 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

相关问题