/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = TeamCoach::find(); $teamCoachTable = TeamCoach::tableName(); $coachTable = Coach::tableName(); $teamTable = Team::tableName(); $seasonTable = Season::tableName(); $query->joinWith(['coach' => function ($query) use($coachTable) { $query->from(['coach' => $coachTable]); }]); $query->joinWith(['team' => function ($query) use($teamTable) { $query->from(['team' => $teamTable]); }]); $query->joinWith(['season' => function ($query) use($seasonTable) { $query->from(['season' => $seasonTable]); }]); $dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => ['pageSize' => 10]]); $addSortAttributes = ["coach.name", "team.name", "season.name"]; foreach ($addSortAttributes as $addSortAttribute) { $dataProvider->sort->attributes[$addSortAttribute] = ['asc' => [$addSortAttribute => SORT_ASC], 'desc' => [$addSortAttribute => SORT_DESC]]; } if (!($this->load($params) && $this->validate())) { return $dataProvider; } $query->andFilterWhere(["{$teamCoachTable}.id" => $this->id, 'team_id' => $this->team_id, 'season_id' => $this->season_id, 'coach_id' => $this->coach_id, 'is_main' => $this->is_main]); $query->andFilterWhere(['like', 'coach.name', $this->getAttribute('coach.name')])->andFilterWhere(['like', 'team.name', $this->getAttribute('team.name')])->andFilterWhere(['like', 'season.name', $this->getAttribute('season.name')]); return $dataProvider; }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Coach::find(); $country = new Country(); $coachTable = Coach::tableName(); $countryTable = Country::tableName(); $query->joinWith(['country' => function ($query) use($countryTable) { $query->from(['country' => $countryTable]); }]); $dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => ['pageSize' => 10]]); // enable sorting for the related columns $addSortAttributes = ["country.name"]; foreach ($addSortAttributes as $addSortAttribute) { $dataProvider->sort->attributes[$addSortAttribute] = ['asc' => [$addSortAttribute => SORT_ASC], 'desc' => [$addSortAttribute => SORT_DESC]]; } if (!($this->load($params) && $this->validate())) { return $dataProvider; } $query->andFilterWhere(["{$coachTable}.id" => $this->id, 'birthday' => $this->birthday, 'country_id' => $this->country_id, 'created_at' => $this->created_at, 'updated_at' => $this->updated_at]); $query->andFilterWhere(['like', "{$coachTable}.name", $this->name])->andFilterWhere(['like', 'slug', $this->slug])->andFilterWhere(['like', 'position', $this->position])->andFilterWhere(['like', 'notes', $this->notes])->andFilterWhere(['like', 'player_carrer', $this->player_carrer])->andFilterWhere(['like', 'coach_carrer', $this->coach_carrer])->andFilterWhere(['like', 'image', $this->image])->andFilterWhere(['like', 'country.name', $this->getAttribute('country.name')]); return $dataProvider; }
/** * Display list of coaches in json format * * @param string $query Query for search * @return mixed Json data */ public function actionCoachList($query = null) { if ($query == null) { throw new NotFoundHttpException('The requested page does not exist.'); } $search = urldecode($query); $query = new Query(); $query->select("id, name")->from(Coach::tableName())->where(['like', 'name', $search])->orderBy('name')->limit(10); $command = $query->createCommand(); $data = $command->queryAll(); $data = array_values($data); $out = []; foreach ($data as $coach) { $out[] = ['value' => $coach['id'], 'text' => $coach['name']]; } header("Content-type: text/html; charset=utf-8"); echo Json::encode($out); }