Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Yii2 未定义变量:model2_Yii2 - Fatal编程技术网

Yii2 未定义变量:model2

Yii2 未定义变量:model2,yii2,Yii2,我有两个模型,将以一种形式加载。 但是当我想访问第二个模型时,出现了一个错误,上面写着“UndefinedVariable:model2” 请帮忙 这是控制器 InventoryController.php public function actionInsert() { $connection = \Yii::$app->db; $transaction = $connection->beginTransaction(); $model = new Inv

我有两个模型,将以一种形式加载。 但是当我想访问第二个模型时,出现了一个错误,上面写着“UndefinedVariable: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() ?>

您的url是:
localhost:81/posrkidev/web/index.php/inventory/create

你应该把所有的代码从

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

或者尝试以下url:
localhost:81/posrkidev/web/index.php/inventory/insert

您能告诉我们文件和行中发生错误的位置$model2的导出在传递到创建之前,告诉resultUndefined变量:model2$this->params['breadcrumbs']=$this->title;?>$model3有效吗?请显示您的url,如
localhost/yourapp/controller/action
$model3还未定义变量:model3 url