public static function addFlights($competition, $registrations) { $flight_size = $competition->flight_size ? $competition->flight_size : Competition::FLIGHT_SIZE_DEFAULT; $position = Flight::find()->andWhere(['competition_id' => $competition->id])->max('position'); if (!intval($position) > 0) { $position = 0; } $position++; $count = $flight_size; // we collect the teams for which we don't have a registration yet $team_ids = []; foreach ($registrations->each() as $registration) { $team_ids[$registration->team_id] = $registration->team_id; } foreach (Team::find()->andWhere(['id' => $team_ids])->each() as $team) { if ($count >= $flight_size) { $count = 0; $flight = new Flight(); $flight->position = $position++; $flight->save(); $flight->refresh(); } foreach ($team->getRegistrations()->each() as $registration) { $registration->flight_id = $flight->id; $registration->save(); } $count++; } }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Flight::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); if (!($this->load($params) && $this->validate())) { return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'competition_id' => $this->competition_id, 'position' => $this->position]); $query->andFilterWhere(['like', 'note', $this->note]); return $dataProvider; }
public static function addFlights($competition, $registrations) { $flight_size = $competition->flight_size ? $competition->flight_size : Competition::FLIGHT_SIZE_DEFAULT; $position = Flight::find()->andWhere(['competition_id' => $competition->id])->max('position'); if (!intval($position) > 0) { $position = 0; } $position++; $count = $flight_size; foreach ($registrations->each() as $registration) { if ($count >= $flight_size) { $count = 0; $flight = new Flight(); $flight->position = $position++; $flight->save(); } $registration->flight_id = $flight->id; $registration->save(); $count++; } }