Yii2 DepDrop 卡蒂克 [英] Yii2 DepDrop kartik

查看:14
本文介绍了Yii2 DepDrop 卡蒂克的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我正在尝试使用 Kartik Depdrop 小部件,但收到此错误 500(内部服务器错误)

Ok I am trying to use the Kartik Depdrop widget, but get this error 500 (Internal Server Error)

我有父模型品牌和子模型 CarModel

i have parent model Brand and submodel CarModel

这是我的文件:

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'=>'']);
}

模型 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;

}

和我的视图文件

<?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'])
        ]
    ]);

?>

我的错误是什么?

推荐答案

控制器内部的代码

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

以下变化

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

并将getSubCatList方法定义为静态方法

public static function getSubCatList($cat_id)

这篇关于Yii2 DepDrop 卡蒂克的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆