Yii2更新两个相关模型不会显示第二个的数据 [英] Yii2 updating two related models does not show data of the second
问题描述
在hasToMany关系中,我有两个相关模型。第一个是 Invoices
,第二个是 InvoiceItems
。作为一个原型过程,我试图使用以下命令通过 InvoicesController
的 update
视图将两个模型结合起来 actionUpdate
中的 InvoicesController
中的代码:
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,
]);
}
}
更新中的以下内容
视图:
<div class="invoices-update">
<h1><?= Html::encode($this->title) ?></h1>
<?= $this->render('_form', [
'model' => $model,
'invoiceItems' => $invoiceItems,
]) ?>
</div>
最后,在 _form
视图中,以下内容:
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>
以上代码成功将数据保存在 invoice_items
表格。 但是,更新视图表单的 InvoiceItems
模型具有空白字段。即 item_id
, unit_id
和 qty
为空保存后的表格。
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.
推荐答案
似乎更新视图为空,因为您渲染并清空了InvoceItmes ..
Seems the update view is empty because you render and empty InvoceItmes ..
$invoiceItems = new InvoiceItems();
您在else部分中呈现更新视图,并且此部分中的值未加载
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,
]);
}
用于填充$ invoceItems的数据,您需要类似
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..
这篇关于Yii2更新两个相关模型不会显示第二个的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!