/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Cities::find(); //->with('country'); $query->joinWith(['country']); $dataProvider = new ActiveDataProvider(['query' => $query]); // Lets do the same with country now $dataProvider->sort->attributes['country'] = ['asc' => ['country.name' => SORT_ASC], 'desc' => ['country.name' => SORT_DESC]]; $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to return any records when validation fails // $query->where('0=1'); return $dataProvider; } $query->andFilterWhere(['like', 'country_code', $this->country_code])->andFilterWhere(['like', 'city_code', $this->city_code])->andFilterWhere(['like', 'city_name', $this->city_name]); $query->andFilterWhere(['like', 'country.name', $this->country]); return $dataProvider; }
/** * @return \yii\db\ActiveQuery */ public function getCities() { return $this->hasMany(Cities::className(), ['country_code' => 'code']); }
/** * Finds the Cities model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param string $id * @return Cities the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Cities::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }