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

妖精的绣舞 提交于 2020-01-06 19:55:34

问题


I have two related models in hasToMany relation. The first is Invoices and the second is InvoiceItems. As a prototype process, I'm trying to conjugate the two models through the update view of the InvoicesController using the following code in actionUpdate of InvoicesController:

...
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,
            ]);
        }
    }

The following in update view:

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

Finally, the following in _form view:

<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>

The above code succeeded in saving data in invoice_items table. However, the update view form has empty fields for InvoiceItems model. i.e item_id, unit_id and qty are empty in the update form after saving.

Notice: This is initial code, i.e I will work later to be able to add many items to the invoice, but now I just try to have one item related to the invoice.


回答1:


Seems the update view is empty because you render and empty InvoceItmes ..

 $invoiceItems = new InvoiceItems();

you render update view in else section and this section the value posted are not loaded

    $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,
        ]);
    }

for populate the data for $invoceItems you need somethings like

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

Obviously this is for a single model.. you must refactory form more than one..



来源:https://stackoverflow.com/questions/34839910/yii2-updating-two-related-models-does-not-show-data-of-the-second

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!