2015-04-23 6 views
6

Ok Próbuję użyć widżetu Kartik Depdrop, ale ten błąd 500 (Internal Server Error)Yii2 DepDrop Kartik

mam dominującym modelem marki i submodel CarModel

Oto moje pliki:

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

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; 

} 

i mój plik 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']) 
     ] 
    ]); 

?> 

jaki jest mój błąd?

+1

byłoby przydatne, jeśli można zakładać co wewnętrznego s błąd erver, publikując wyjście dziennika Yii2 (../runt/logs/app.log) – Manquer

+0

wydrukuj i pokaż nam swoje '$ out' i' $ _POST ['depdrop_parents'] '. –

+0

depdrop_parents [0]: 1 1 jest moim rodzicem id i tym moim $ out array (2) {[0] => array (2) {["id"] => string (1) "1" [ "name"] => string (3) "318"} [1] => array (2) {["id"] => string (1) "2" ["name"] => string (3) " 320 "}} –

Odpowiedz

1

Kod wewnątrz sterownika

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

następujące zmiany

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

i zdefiniować getSubCatList metoda metoda statyczna

public static function getSubCatList($cat_id) 
+0

500 (Wewnętrzny błąd serwera) ponownie ( –

+0

umieścić tutaj zawartość pliku dziennika (** runtime/logs/app.log **) – Reza1607

+0

Dodałem użycie yii \ helpers \ Json i działało))) dzięki) –