/**
  * @param string|null $q
  * @param int|null    $id
  * @param int|null    $country_id
  * @param int|null    $region_id
  *
  * @return array
  */
 public function actionCities($q = null, $id = null, $country_id = null, $region_id = null)
 {
     if ($id) {
         return ['results' => ['id' => (int) $id, 'text' => City::findOne((int) $id)->name]];
     }
     if ($q) {
         $data = (new Query())->select('c.id AS id, c.name AS text')->from(CityName::tableName() . ' cn')->innerJoin(City::tableName() . ' c', 'c.id = cn.city_id')->where(['like', 'cn.name', $q . '%', false])->groupBy('cn.city_id')->limit(50);
         if ($country_id) {
             $data->andWhere(['cn.country_id' => $country_id]);
         }
         if ($region_id) {
             $data->andWhere(['cn.region_id' => $region_id]);
         }
     } else {
         $data = (new Query())->select('id, name AS text')->from(City::tableName())->limit(50);
         if ($country_id) {
             $data->andWhere(['country_id' => $country_id]);
         }
         if ($region_id) {
             $data->andWhere(['region_id' => $region_id]);
         }
     }
     return ['results' => array_values($data->createCommand()->queryAll())];
 }
Exemple #2
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getCitiesNames()
 {
     return $this->hasMany(CityName::className(), ['country_id' => 'id']);
 }