2015-04-23 20 views
6

Ok Sto cercando di utilizzare il widget Kartik Depdrop, ma questo errore 500 (Internal Server Error)Yii2 DepDrop kartik

devo modello di genitore Marca e sottomodello Carmodel

Qui ci sono i miei file:

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

modello 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; 

} 

e il mio 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']) 
     ] 
    ]); 

?> 

qual è il mio errore?

+1

sarebbe utile se si può postare che interno s errore di erver che stai avendo inviando l'output del registro Yii2 (../runtime/logs/app.log) – Manquer

+0

stampa e mostraci i tuoi '$ out' e' $ _POST ['depdrop_parents'] '. –

+0

depdrop_parents [0]: 1 1 è il mio genitore id e questo mio $ out array (2) {[0] => array (2) {["id"] => stringa (1) "1" [ "name"] => string (3) "318"} [1] => array (2) {["id"] => stringa (1) "2" ["nome"] => stringa (3) " 320 "}} –

risposta

1

Il codice all'interno del controllore

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

la seguente modifica

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

e definire getSubCatList metodo per metodo statico

public static function getSubCatList($cat_id) 
+0

di nuovo (Errore interno server) di nuovo ( –

+0

metti qui il contenuto del registro file (** runtime/logs/app.log **) – Reza1607

+0

Ho aggiunto l'uso yii \ helpers \ Json e ha funzionato))) grazie) –