0

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

...
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_items表中。但是,更新视图表单的模型字段为空InvoiceItems。即item_id,保存后在更新表单unit_idqty为空。

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

4

1 回答 1

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

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

于 2016-01-17T16:12:41.787 回答