我有2个模型将以一种形式加载。 但是当我想访问第二个模型时,就会出现错误"未定义的变量:model2" 请帮忙。
这是控制器 InventoryController.php
public function actionInsert() {
$connection = \Yii::$app->db;
$transaction = $connection->beginTransaction();
$model = new Inventory();
$model2 = new \app\models\Unitofmeasurement();
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
$command = $connection->createCommand('{call usp_M_Inventory#Transaksi(:ID_Item,:Item_Name, :IDMom, :Item_Price, :ID_InvCategory,:Item_PIC1,
:Item_PIC2,:Item_active, :UserInventory, :ID_Mom, :Satuan_Beli, :Qty_Beli, :Satuan_Jual,:Qty_Jual, :ActiveMOM, :UserMOM)}');
$ID_Item = $model->ID_Item;
$Item_Name = $model->Item_Name;
$IDMom = $model->IDMom;
$Item_Price = $model->Item_Price;
$ID_InvCategory = $model->ID_Inv_Category;
$Item_PIC1 = $model->Item_PIC1;
$Item_PIC2 = $model->Item_PIC2;
$Item_active = $model->Item_active;
$UserInventory = Yii::$app->user->identity->username;
$ID_Mom = $model2->ID_Mom;
$Satuan_Beli = $model2->Satuan_Beli;
$Qty_Beli = $model2->Qty_Beli;
$Satuan_Jual = $model2->Satuan_Jual;
$Qty_Jual = $model2->Qty_Jual;
$ActiveMOM = $model2->Active;
$UserMOM = Yii::$app->user->identity->username;
if ($command->execute() == 0) {
$transaction->commit();
} else {
$transaction->rollBack();
foreach ($model->getErrors() as $key => $message) {
Yii::$app->session->setFlash('error', $message);
}
}
return $this->redirect(['view', 'id' => $model->ID_Item]);
} else {
return $this->render('create', array(
'model' => $model,
'model2' => $model2,
'model3' => $model3,
'model4' => $model4,
'model5' => $model5,
'model6' => $model6,
'model7' => $model7,
));
}
这是创建视图 create.php
<h1><?= Html::encode($this->title) ?></h1>
<?=
$this->render('_form', [
'model' => $model,
'model2' => $model2,
'model3' => $model3,
'model4' => $model4,
'model5' => $model5,
'model6' => $model6,
'model7' => $model7,
])
?>
这是表格
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'ID_Item')->textInput() ?>
<?= $form->field($model, 'Item_Name')->textInput() ?>
<?= $form->field($model, 'ID_Mom')->textInput() ?>
<?= $form->field($model, 'Item_Price')->textInput() ?>
<?= $form->field($model, 'ID_Inv_Category')->textInput() ?>
<?= $form->field($model, 'Item_PIC1')->textInput() ?>
<?= $form->field($model, 'Item_PIC2')->textInput() ?>
<?=
$form->field($model, 'Item_active')->widget(SwitchInput::classname(), [
'pluginOptions' => [
'onText' => 'Active',
'offText' => 'Not Active',
]
])
?>
<?=$form->field($model2,'ID_Mom')->textInput() ?>
答案 0 :(得分:1)
您的网址是:public function actionInsert() {
...
}
你应该把所有代码都放在
中 public function actionCreate() {
$connection = \Yii::$app->db;
$transaction = $connection->beginTransaction();
$model = new Inventory();
$model2 = new \app\models\Unitofmeasurement();
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
$command = $connection->createCommand('{call usp_M_Inventory#Transaksi(:ID_Item,:Item_Name, :IDMom, :Item_Price, :ID_InvCategory,:Item_PIC1,
:Item_PIC2,:Item_active, :UserInventory, :ID_Mom, :Satuan_Beli, :Qty_Beli, :Satuan_Jual,:Qty_Jual, :ActiveMOM, :UserMOM)}');
$ID_Item = $model->ID_Item;
$Item_Name = $model->Item_Name;
$IDMom = $model->IDMom;
$Item_Price = $model->Item_Price;
$ID_InvCategory = $model->ID_Inv_Category;
$Item_PIC1 = $model->Item_PIC1;
$Item_PIC2 = $model->Item_PIC2;
$Item_active = $model->Item_active;
$UserInventory = Yii::$app->user->identity->username;
$ID_Mom = $model2->ID_Mom;
$Satuan_Beli = $model2->Satuan_Beli;
$Qty_Beli = $model2->Qty_Beli;
$Satuan_Jual = $model2->Satuan_Jual;
$Qty_Jual = $model2->Qty_Jual;
$ActiveMOM = $model2->Active;
$UserMOM = Yii::$app->user->identity->username;
if ($command->execute() == 0) {
$transaction->commit();
} else {
$transaction->rollBack();
foreach ($model->getErrors() as $key => $message) {
Yii::$app->session->setFlash('error', $message);
}
}
return $this->redirect(['view', 'id' => $model->ID_Item]);
} else {
return $this->render('create', array(
'model' => $model,
'model2' => $model2,
'model3' => $model3,
'model4' => $model4,
'model5' => $model5,
'model6' => $model6,
'model7' => $model7,
));
}
动作创建:
localhost:81/posrkidev/web/index.php/inventory/insert
或尝试此网址:{{1}}