R
R
rusline2017-02-23 11:52:57
Yii
rusline, 2017-02-23 11:52:57

Did I pull the data from the database correctly, as indicated in the DepDrop widget?

Data does not come from the database, I work with the DepDrop widget.
It turns out I have a frontend/signup.php file

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

And siteController contains the path to the siteController database
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');
    }

Well, there is also a connection with the database in the models region, city.
For example, the code inside them looks like this
namespace frontend\models;

use yii\db\ActiveRecord;

class Region extends ActiveRecord
{

}

I just don’t understand what I ’m doing wrong with the database, that it doesn’t display records in the
list
// the getSubCatList function will query the database based on the
// cat_id and return an array like below:
// [
// ['id'=>'', 'name'=>''],
// ['id '=>'', 'name'=>'']
// ]

And the tables I have for regions are region_id | country_id | name

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question