@rusline

Правильно ли я вывел данные из базы данных, как указана в виджете DepDrop?

Не приходят данные из базы данных, работаю с виджетом DepDrop
У меня выходит есть файл frontend/signup.php
$form->field($model, 'country')->dropDownList([
                '3159'=>'Россия'
                ], ['prompt'=>'Выберите страну'])

<?= $form->field($model, 'region')->widget(DepDrop::classname(), [
                'options' => ['region_id'=>'region_id'],
                'pluginOptions'=>[
                    'depends'=>['country_id'],
                    'placeholder' => 'Выберите страну',
                    'url'=>Url::to(['/site/region'])
                    ]
                ]);
                ?>

                <?= $form->field($model, 'city')->widget(DepDrop::classname(), [
                    'pluginOptions'=>[
                        'depends'=>['country_id', 'region_id'],
                        'placeholder'=>'Выберите регион',
                        'url'=>Url::to(['city'])
                    ]
                ]); 
                ?>

И в siteController указан путь к бд siteController
public function actionRegion() {
        $out = [];
        if (isset($_POST['depdrop_parents'])) {
            $parents = $_POST['depdrop_parents'];
            if ($parents != null) {
                $country_id = $parents[0];
                $out = self::getRegionList($country_id); 
                echo Json::encode(['output'=>$out, 'selected'=>'']);
                return;
            }
        }
        echo Json::encode(['output'=>'', 'selected'=>'']);
    }

    public function actionCity()
    {
       $out = [];
        if (isset($_POST['depdrop_parents'])) 
        {
            $ids = $_POST['depdrop_parents'];
            $country_id = empty($ids[0]) ? null : $ids[0];
            $region_id = empty($ids[1]) ? null : $ids[1];
            if ($country_id != null) {
               $data = self::getCityList($country_id, $region_id);
               
               echo Json::encode(['output'=>$data['out'], 'selected'=>$data['selected']]);
               return;
            }
        }
        echo Json::encode(['output'=>'', 'selected'=>'']);
    }

public function getRegionList($country_id){
        $model = frontend\models\Region::find()->where(['country_id'=> $country_id])->asArray()->all();
        return ArrayHelper::map($model, 'region_id', 'name');
    }

     public function getCityList($country_id, $region_id){
        $model = frontend\models\City::find()->where(['country_id'=> $country_id, 'region_id'=> $region_id])->asArray()->all();
        return ArrayHelper::map($model, 'city_id', 'name');
    }

Ну и также есть связь с бд в models region, city
Примеру код внутри них выглядит так
namespace frontend\models;

use yii\db\ActiveRecord;

class Region extends ActiveRecord
{

}

ТОлько не пойму, что я делаю не так с базой данной, что она не выводить записи в список
На сайте виджета указана, что надо делать так вызов из бд
// the getSubCatList function will query the database based on the
// cat_id and return an array like below:
// [
// ['id'=>'', 'name'=>''],
// ['id'=>'', 'name'=>'']
// ]

И таблицы у меня к регионы такие region_id | country_id | name
  • Вопрос задан
  • 356 просмотров
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы