Yii2 更新两个相关模型不显示第二个的数据

Yii2 updating two related models does not show data of the second

我在 hasToMany 关系中有两个相关模型。第一个是 Invoices,第二个是 InvoiceItems。作为原型过程,我试图通过 InvoicesControllerupdate 视图使用 InvoicesControlleractionUpdate 中的以下代码来结合两个模型:

...
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 视图中的以下内容:

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

最后,_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_itemstable中的数据。 但是,更新视图表单有 InvoiceItems 模型的空字段。即 item_idunit_idqty 在保存后的更新表单中为空。

注意:这是初始代码,即我稍后会工作,以便能够将许多项目添加到发票中,但现在我只尝试让一个项目与发票。

似乎更新视图是空的,因为您渲染并清空了 InvoceItmes ..

 $invoiceItems = new InvoiceItems();

您在 else 部分呈现更新视图,并且此部分未加载发布的值

    $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.');
    }
}

显然这是针对单个模型的。您必须重构多个模型。