简体   繁体   English

Yii2 DepDrop kartik

[英]Yii2 DepDrop kartik

Ok I am trying to use the Kartik Depdrop widget, but get this error 500 (Internal Server Error) 好的我正在尝试使用Kartik Depdrop小部件,但得到此错误500(内部服务器错误)

i have parent model Brand and submodel CarModel 我有父模型品牌和子模型CarModel

Here are my files: 这是我的文件:

CarsController.php CarsController.php

public function actionSubcat() {
    $out = [];
    if (isset($_POST['depdrop_parents'])) {
        $parents = $_POST['depdrop_parents'];
        if ($parents != null) {
            $cat_id = $parents[0];
            $out = self::getSubCatList($cat_id);
            // the getSubCatList function will query the database based on the
            // cat_id and return an array like below:
            // [
            //    ['id'=>'<sub-cat-id-1>', 'name'=>'<sub-cat-name1>'],
            //    ['id'=>'<sub-cat_id_2>', 'name'=>'<sub-cat-name2>']
            // ]
            echo Json::encode(['output'=>$out, 'selected'=>'']);
            return;
        }
    }
    echo Json::encode(['output'=>'', 'selected'=>'']);
}

model Cars.php 模型Cars.php

public function getSubCatList($cat_id)
{
    $data=\common\models\CarModel::find()
       ->where(['brand_id'=>$cat_id])
       ->select(['id','name_ru AS name' ])->asArray()->all();

    return $data;

}

and my viw file 和我的viw文件

<?php $catList = ArrayHelper::map(Brand::find()->all(),'id','name_ru'); ?>

<?=  $form->field($model, 'brand_id')->dropDownList($catList, 
    [
        'prompt' => 'Select brand',
        'id'=>'brand_id-id'
    ]);
?>

<?=$form->field($model, 'car_model_id')->widget(DepDrop::classname(), [
         'options' => ['id'=>'car_model_id-id'],
         'pluginOptions'=>[
             'depends'=>['brand_id-id'],
             'placeholder' => 'Select...',
             'url' => Url::to(['subcat'])
        ]
    ]);

?>

what is my mistake? 我的错是什么?

The code inside the controller 控制器内的代码

$out = self::getSubCatList($cat_id);

the following change 以下变化

$out = Cars::getSubCatList($cat_id);

and define getSubCatList method to static method 并将getSubCatList方法定义为静态方法

public static function getSubCatList($cat_id)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM