/** * Создает DataProvider на основе переданных данных * @param $params - параметры * @return ActiveDataProvider */ public function search($params) { $query = City::find(); $dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => ['pageSize' => $this::COUNT]]); $this->load($params); // Если валидация не пройдена, то ничего не выводить if (!$this->validate()) { $query->where('0=1'); return $dataProvider; } // Фильтрация $query->andFilterWhere(['id' => $this->id, 'country_id' => $this->country_id, 'biggest_city' => $this->biggest_city]); $query->andFilterWhere(['like', 'city', $this->city])->andFilterWhere(['like', 'state', $this->state])->andFilterWhere(['like', 'region', $this->region]); return $dataProvider; }
/** * Поиск населенного пункта по названию или ID * @param null $q - часть названия или целиком * @param null $id - ID населенного пункта ( в случае поиска по ID * @return array [ID => 'Город (Район, Регион)'] */ public function actionFind($q = null, $id = null) { \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; $out = ['results' => ['id' => '', 'text' => '']]; if (!is_null($q)) { $query = new Query(); $query->select('id, city, state, region')->from('lb_city')->where(['like', 'city', $q])->limit(1000); $command = $query->createCommand(); $data = $command->queryAll(); $format_data = []; foreach ($data as $d) { $format_data[] = ['id' => $d['id'], 'text' => $d['city'] . " (" . $d['state'] . ", " . $d['region'] . ")"]; } $out['results'] = array_values($format_data); } elseif ($id > 0) { $out['results'] = ['id' => $id, 'text' => City::find($id)->city]; } return $out; }