/** * Displays a single Games model. * @param integer $id * @return mixed */ public function actionView($id) { $this->layout = 'main-full'; $model = $this->findModel($id); $gameData['home'] = GamesPlayers::find()->where(['game_id' => $model->id, 'team_id' => $model->home_id])->all(); $gameData['guest'] = GamesPlayers::find()->where(['game_id' => $model->id, 'team_id' => $model->guest_id])->all(); $gameData['mainTeam'] = Teams::find()->select('id')->where(['name' => Yii::$app->params['main-team']])->one(); $gameData['nextGame'] = Games::find()->select('id')->where(['home_id' => $gameData['mainTeam']->id])->orWhere(['guest_id' => $gameData['mainTeam']->id])->andWhere(['>', 'date', $model->date])->one(); $gameData['prevGame'] = Games::find()->select('id')->where(['home_id' => $gameData['mainTeam']->id])->orWhere(['guest_id' => $gameData['mainTeam']->id])->andWhere(['<', 'date', $model->date])->orderBy('date DESC')->one(); $dataProvider['gamePlayersHome'] = new ActiveDataProvider(['query' => GamesPlayers::find()->where(['game_id' => $model->id, 'team_id' => $model->home_id])]); $dataProvider['gamePlayersGuest'] = new ActiveDataProvider(['query' => GamesPlayers::find()->where(['game_id' => $model->id, 'team_id' => $model->guest_id])]); return $this->render('view', ['model' => $model, 'gameData' => $gameData, 'dataProvider' => $dataProvider]); }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = GamesPlayers::find(); // add conditions that should always apply here $dataProvider = new ActiveDataProvider(['query' => $query]); $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; } // grid filtering conditions $query->andFilterWhere(['id' => $this->id, 'game_id' => $this->game_id, 'player_id' => $this->player_id, 'team_id' => $this->team_id, 'time' => $this->time]); return $dataProvider; }
public function actionAdd() { $data = Yii::$app->request->post('data'); $players = ArrayHelper::getColumn(GamesPlayers::find()->select('player_id')->where(['game_id' => $data['game'], 'team_id' => $data['team']])->asArray()->all(), 'player_id'); foreach ($data['array'] as $player) { if (!in_array($player, $players)) { $model = new GamesPlayers(); $model->game_id = $data['game']; $model->team_id = $data['team']; $model->player_id = $player; $model->save(); } } }