【问题标题】:Yii2 updating two related models does not show data of the secondYii2更新两个相关模型不显示第二个的数据
【发布时间】:2016-01-18 00:28:25
【问题描述】:

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

...
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_idunit_idqty在保存后的更新表单中为空。

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

【问题讨论】:

    标签: activerecord yii2 active-form


    【解决方案1】:

    似乎更新视图是空的,因为您渲染并清空 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.');
        }
    }
    

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

    【讨论】:

    • 好吧!我该怎么做才能用数据填充它?
    • 当我尝试$invoiceItems = $model-&gt;getInvoiceItems(); 时返回错误:Calling unknown method: common\models\InvoiceItemsQuery::formName()
    • 非常感谢。如果我需要找到发票的所有项目,您能否提示我。我尝试使用 findAll 而不是 findOne 但它返回错误:Call to a member function formName() on array
    • FindAll 是对的,但请记住,结果是模型的集合,而不是单个模型。然后访问您需要的每个模型或迭代或索引..
    • 非常有用,我想提一下应该在视图中发生循环,即_form
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-12-27
    • 1970-01-01
    • 1970-01-01
    • 2020-08-19
    • 1970-01-01
    • 2017-10-21
    • 1970-01-01
    相关资源
    最近更新 更多