/**
  * Saves the links between a match and the teams playing in it
  * @param $match
  * @return void
  */
 public function SaveTeams(Match $match)
 {
     if (!$match->GetId()) {
         return;
     }
     $match_id = Sql::ProtectNumeric($match->GetId());
     $match_team_ids = array();
     if ($match->GetHomeTeamId()) {
         $match_team_ids[] = $this->EnsureTeamInMatch($match, $match->GetHomeTeamId(), TeamRole::Home());
     }
     $is_tournament = $match->GetMatchType() == MatchType::TOURNAMENT;
     $tournament = null;
     if ($is_tournament) {
         require_once "stoolball/team-manager.class.php";
         $team_manager = new TeamManager($this->GetSettings(), $this->GetDataConnection());
         # For a tournament, attempt to save the maxiumum number of teams
         $sql = "UPDATE nsa_match SET max_tournament_teams = " . Sql::ProtectNumeric($match->GetMaximumTeamsInTournament(), true, false) . ",\r\n                    tournament_spaces = " . Sql::ProtectNumeric($match->GetSpacesLeftInTournament(), true, false) . "\r\n                    WHERE match_id = {$match_id}";
         $this->GetDataConnection()->query($sql);
     }
     $away_teams = $match->GetAwayTeams();
     foreach ($away_teams as $away_team) {
         $away_team_id = $away_team->GetId();
         # If this is a tournament we may be adding teams based on the name rather than the ID
         if (!$away_team_id and $is_tournament) {
             if (is_null($tournament)) {
                 # Make sure we have full details for the tournament, in order to build any tournament teams
                 $this->ReadByMatchId(array($match->GetId()));
                 $tournament = $this->GetFirst();
             }
             $away_team_id = $team_manager->SaveOrMatchTournamentTeam($tournament, $away_team);
         }
         # Ensure we now have an ID, or the following queries will be invalid
         if ($away_team_id) {
             $match_team_ids[] = $this->EnsureTeamInMatch($match, $away_team_id, TeamRole::Away());
         }
     }
     # See if there are any other teams for this match, aside from those we've just confirmed or added.
     # Those will be teams just deleted.
     $match_table = $this->GetSettings()->GetTable('Match');
     $mt = $this->GetSettings()->GetTable('MatchTeam');
     $stats = $this->GetSettings()->GetTable('PlayerMatch');
     $sql = "SELECT match_team_id, team.team_id, team.team_type \r\n\t\t          FROM {$mt} INNER JOIN nsa_team AS team ON {$mt}.team_id = team.team_id\r\n\t\t          WHERE match_id = {$match_id}";
     if (count($match_team_ids)) {
         $sql .= " AND match_team_id NOT IN (" . implode(",", $match_team_ids) . ")";
     }
     $result = $this->GetDataConnection()->query($sql);
     $delete_match_team_ids = array();
     $delete_team_ids = array();
     while ($row = $result->fetch()) {
         $delete_match_team_ids[] = $row->match_team_id;
         if ($is_tournament and $row->team_type == Team::ONCE) {
             $delete_team_ids[] = $row->team_id;
         }
     }
     if (count($delete_match_team_ids)) {
         # If there are, delete them and their dependent records
         $delete_match_team_ids = implode(",", $delete_match_team_ids);
         $batting = $this->GetSettings()->GetTable('Batting');
         $bowling = $this->GetSettings()->GetTable('Bowling');
         $this->LoggedQuery("DELETE FROM {$batting} WHERE match_team_id IN ({$delete_match_team_ids})");
         $this->LoggedQuery("DELETE FROM {$bowling} WHERE match_team_id IN ({$delete_match_team_ids})");
         $this->LoggedQuery("DELETE FROM {$stats} WHERE match_team_id IN ({$delete_match_team_ids})");
         $this->LoggedQuery("DELETE FROM {$mt} WHERE match_team_id IN ({$delete_match_team_ids})");
         $this->RemovePlayerOfTheMatchIfTeamRemoved($match_id, "player_of_match_id");
         $this->RemovePlayerOfTheMatchIfTeamRemoved($match_id, "player_of_match_home_id");
         $this->RemovePlayerOfTheMatchIfTeamRemoved($match_id, "player_of_match_away_id");
     }
     # make sure the player statistics have the correct opposition
     $this->GetDataConnection()->query("UPDATE {$stats} SET opposition_id = NULL WHERE match_id = {$match_id}");
     foreach ($match_team_ids as $match_team_id) {
         $sql = "SELECT team_id\r\n\t\t\t\t\t\tFROM {$mt} INNER JOIN {$match_table} m ON {$mt}.match_id = m.match_id\r\n\t\t\t\t\t\tWHERE m.match_id = (SELECT match_id FROM {$mt} WHERE match_team_id = {$match_team_id})\r\n\t\t\t\t\t\tAND {$mt}.match_team_id != {$match_team_id}\r\n\t\t\t\t\t\tAND team_role IN (" . TeamRole::Home() . ", " . TeamRole::Away() . ")";
         $data = $this->GetDataConnection()->query($sql);
         $data_row = $data->fetch();
         if ($data_row) {
             $this->GetDataConnection()->query("UPDATE {$stats} SET opposition_id = {$data_row->team_id} WHERE match_team_id = {$match_team_id}");
         }
     }
     # If we found any once-only tournament teams that were removed, delete them
     if ($is_tournament) {
         if (count($delete_team_ids)) {
             $team_manager->Delete($delete_team_ids);
         }
         unset($team_manager);
     }
     $result->closeCursor();
 }