未定义的变量:yii2

问题描述 投票:0回答:5

enter image description here获取错误,当我试图创建使用yii2-dynamicform动态表单。在创建方法时它工作正常,但在更新时显示错误。我有两个表一个1.vendors&2.vendors_more_categories

关系是1 *厂商及vendors_more_categories之间我只是吹罚https://github.com/wbraganca/yii2-dynamicform此链接。

<?php

namespace app\controllers;
namespace backend\controllers;
use Yii;
use app\models\Vendors;
use app\models\VendorsSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\web\UploadedFile;
use yii\filters\AccessControl;
use app\models\VendorsMoreCategories;
use backend\models\Model;
use yii\web\Response;
use yii\widgets\ActiveForm;
use yii\helpers\ArrayHelper;
/**
 * VendorsController implements the CRUD actions for Vendors model.
 */
class VendorsController extends Controller
{
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'only' => ['index','create', 'update', 'delete'],
                'rules' => [
                    [
                        'actions' => ['index','create', 'update', 'delete'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['post'],
                ],
            ],
        ];
    }

    /**
     * Lists all Vendors models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new VendorsSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single Vendors model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new Vendors model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new Vendors();
        $modelsVendorsMoreCategories = [new VendorsMoreCategories];
        if($model->load(Yii::$app->request->post())){        

            $modelsVendorsMoreCategories = Model::createMultiple(VendorsMoreCategories::classname());
            Model::loadMultiple($modelsVendorsMoreCategories, Yii::$app->request->post());


            // validate all models
            $valid = $model->validate();
            $valid = Model::validateMultiple($modelsVendorsMoreCategories) && $valid;

            if ($valid) {
                $transaction = \Yii::$app->db->beginTransaction();
                try {
                    if ($flag = $model->save(false)) {
                        foreach ($modelsVendorsMoreCategories as $modelVendorsMoreCategories) {
                            $modelVendorsMoreCategories->vmc_ven_id = $model->ven_id;
                            if (! ($flag = $modelVendorsMoreCategories->save(false))) {
                                $transaction->rollBack();
                                break;
                            }
                        }
                    }
                    if ($flag) {
                        $transaction->commit();

                        $model->file = UploadedFile::getInstance($model, 'file');
                        $save_file = '';
                        if($model->file){
                            $imagename = Vendors::find()->orderBy('ven_id DESC')->one();
                            $imagename=$imagename->ven_id+1;
                            $imagepath = 'images/imgvendors/'; // Create folder under web/uploads/logo
                            $model->ven_business_logo = $imagepath.$imagename.'.'.$model->file->extension;
                            $save_file = 1;
                        }
                        if ($model->save(false)) {
                            if($save_file){
                                $model->file->saveAs($model->ven_business_logo);
                            }
                            return $this->redirect(['view', 'id' => $model->ven_id]);
                        }                        
                    }
                } catch (Exception $e) {
                    $transaction->rollBack();
                }
            }            
        }else {
            return $this->render('create', [
                'model' => $model,
                'modelsVendorsMoreCategories' => (empty($modelsVendorsMoreCategories)) ? [new VendorsMoreCategories] : $modelsVendorsMoreCategories
            ]);
        }
    }

    /**
     * Updates an existing Vendors model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);
        //print_r($model->attributes);

        $modelsVendorsMoreCategories = $model->ven_id;   

        if($model->load(Yii::$app->request->post())){          
            $oldIDs = ArrayHelper::map($modelsVendorsMoreCategories, 'id', 'id');
            $modelsVendorsMoreCategories = Model::createMultiple(VendorsMoreCategories::classname(), $modelsVendorsMoreCategories);
            Model::loadMultiple($modelsVendorsMoreCategories, Yii::$app->request->post());
            $deletedIDs = array_diff($oldIDs, array_filter(ArrayHelper::map($modelsVendorsMoreCategories, 'id', 'id')));

            // validate all models
            $valid = $model->validate();
            $valid = Model::validateMultiple($modelsVendorsMoreCategories) && $valid;

            if ($valid) {
                $transaction = \Yii::$app->db->beginTransaction();
                try {
                    if ($flag = $model->save(false)) {
                        if (! empty($deletedIDs)) {
                            Address::deleteAll(['id' => $deletedIDs]);
                        }
                        foreach ($modelsVendorsMoreCategories as $modelVendorsMoreCategories) {
                            $modelVendorsMoreCategories->vmc_ven_id = $model->ven_id;
                            if (! ($flag = $modelVendorsMoreCategories->save(false))) {
                                $transaction->rollBack();
                                break;
                            }
                        }
                    }
                    if ($flag) {
                        $transaction->commit();
                        $model->file = UploadedFile::getInstance($model, 'file');
                        $save_file = '';
                        if($model->file){
                            $imagepath = 'images/imgvendors/'; // Create folder under web/uploads/logo
                            $model->ven_business_logo = $imagepath.$model->ven_id.'.'.$model->file->extension;
                            $save_file = 1;
                        }

                        if ($model->save(false)) {
                            if($save_file){
                                $model->file->saveAs($model->ven_business_logo);
                            }
                            return $this->redirect(['view', 'id' => $model->ven_id]);
                        }                        

                    }
                } catch (Exception $e) {
                    $transaction->rollBack();
                }
            }

        }else {
            return $this->render('update', [
                'model' => $model,
                'modelsVendorsMoreCategories' => (empty($modelsVendorsMoreCategories)) ? [new VendorsMoreCategories] : $modelsVendorsMoreCategories
            ]);
        }
    }

    /**
     * Deletes an existing Vendors model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the Vendors model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return Vendors the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = Vendors::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
    //Function used for deleting the images
    public function actionDeleteimg($id, $field)
    {

        $img = $this->findModel($id)->$field;
        if($img){
            if (!unlink($img)) {
                return false;
            }
        }

        $img = $this->findModel($id);
        $img->$field = NULL;
        $img->update();

        return $this->redirect(['update', 'id' => $id]);
    }
    //Function used for getting more sub categories for vendor
    public function actionGetSubCategories()
    {
        $mbcid=$_GET['ven_main_category_id'];
        $sbcid=$_GET['ven_sub_category_id'];
        echo $mbcid;
    }
    public function actionLists($id)
    {
        $countVendors = Vendors::find()->where(['ven_contact_person_id' => $id])->count();
        $vendors = Vendors::find()->where(['ven_contact_person_id' => $id])->all();
        if ($countVendors > 0) {
            foreach ($vendors as $vendor) {
                echo "<option value='" . $vendor->ven_id . "'>" . $vendor->ven_company_name . "</option>";
            }
        } else {
            echo "<option></option>";
        }
    }

}
yii2 yii-extensions yii2-advanced-app yii-components
5个回答
0
投票

不是访问modelsVendorsMoreCategories [0](作为数组的一个元素)

  'model'=> $modelsVendorsMoreCategories[0], 

在DinamicForm组件,但是当你更新你的模型通过为$ modelsVendorsMoreCategories这个值

   $modelsVendorsMoreCategories = $model->ven_id;  

(不似是一个数组,你必须确保这个对象包含在0指数与适当的元素的数组))

'modelsVendorsMoreCategories' => (empty($modelsVendorsMoreCategories)) ? 
       [new VendorsMoreCategories] : $modelsVendorsMoreCategories
        ]);

0
投票

$ modelsVendorsMoreCategories应包含actionUpdate方法VendorsMoreCategories模型。在此代码($ modelsVendorsMoreCategories = $模型 - > ven_id)$ modelsVendorsMoreCategories包含$模型 - > ven_id。它是一个整数值,而不是VendorsMoreCategories对象。

卖方模式应该包含在VendorsMoreCategories模型的关系:

class Vendors extends \yii\db\ActiveRecord 
{
    ....
    public function getVendorsMoreCategories() 
    {
        return $this->hasMany(VendorsMoreCategories::className(), 'vendor_id'=>'id']);
    }
}

然后,你应该用你的actionUpdate方法的关系:

$model = $this->findModel($id);

$modelsVendorsMoreCategories = $model->vendorsMoreCategories;
if(!$modelsVendorsMoreCategories) {
    $modelsVendorsMoreCategories = [new VendorsMoreCategories];
}

0
投票

在你actionUpdate您有:

$modelsVendorsMoreCategories = $model->ven_id;

但是,你应该有:

$modelsVendorsMoreCategories = $model->nameOfMyRelation;

为了得到什么是真正的名字,走在你的$模式,并期待这样的事情:

/**
 * @return \yii\db\ActiveQuery
 */
public function getNameOfMyRelation()
{
    return $this->hasMany(VendorsMoreCategories::className(), ['ven_id' => 'id']);
}

如果你没有使这两个表之间的关系的任何功能,写一个。如果您有问题这样做,你可以随时使用gii的模型生成和检查卖方模型(你不需要更换,只是预览代码)。


0
投票

检查create.php文件视图文件夹,从这里_form.php这个文件传递所需的变量为: -

 <?= $this->render('_form', [
    'model' => $model,
    'modelsAddress' => $modelsAddress,
]) ?>

-2
投票

检查您创建针对文件夹中的文件:

控制器:控制器传递参数到create.php

 return $this->render('create', [
            'model' => $model,
            'modelsVendorsMoreCategories' => (empty($modelsVendorsMoreCategories)) ? [new VendorsMoreCategories] : $modelsVendorsMoreCategories
        ]);

查看:create.php

如果你错过了一个参数: 'modelsVendorsMoreCategories'=> $ modelsVendorsMoreCategories。它显示在页面_form.php这个未定义变量错误。

<?= $this->render('_form', [
            'model' => $model,
            'modelsVendorsMoreCategories' =>$modelsVendorsMoreCategories
 ])?>

查看:_form.php这个

$modelsVendorsMoreCategories[0];

前现在它传递参数不及格。

© www.soinside.com 2019 - 2024. All rights reserved.