public function getTeamIdFromGoal($matchId, $playerId) { $sel = new \Selector('PlaysMatchInTeam'); $sel->filter([['playerId', '=', $playerId]]); $sel->filter([['matchId', '=', $matchId]]); $result = $this->select($sel); if (sizeof($result) > 1) { echo '<pre>'; var_dump($result); echo '</pre>'; throw new \Exception('Multiple rows for PlaysMatchInTeam'); } elseif (sizeof($result) < 1) { //echo "No rows for PlaysMatchInTeam, for matchId $matchId, and playerId $playerId<br>"; //If we don't find any rows, we check in which team he is playing $sel = new \Selector('PlaysMatchInTeam'); $sel->filter([['matchId', '=', $matchId]]); $sel->join('PlaysIn', 'teamId', 'teamId'); $sel->filter([['PlaysIn.playerId', '=', $playerId]]); $result = $this->select($sel); if (sizeof($result) > 0) { return $result[0]['teamId']; } else { return null; } //throw new \Exception("No rows for PlaysMatchInTeam, for matchId $matchId, and playerId $playerId"); } return $result[0]['teamId']; }
public function GET($args) { global $database; $sel = new \Selector('Player'); $sort = false; $sortColumn = ''; $sortOrder = ''; $limit = 10; foreach ($_GET as $column => $value) { switch ($column) { case 'id': $sel->filter([['Player.id', '=', $value]]); break; case 'firstname': case 'lastname': if (strlen($value) < 3) { $this->players = []; return; } $value = str_replace('%', '\\%', $value); $value = str_replace('[', '\\[', $value); $value = str_replace(']', '\\]', $value); $value = str_replace('_', '\\_', $value); $value = '%' . $value . '%'; $sel->filter([['Player.' . $column, 'LIKE', $value]]); break; case 'sort': if ($value == 'match') { $sort = true; $sortColumn = 'playedMatches'; $sel->join('PlaysMatchInTeam', 'id', 'playerId'); $sel->group('Player.id'); $sel->select(['Player.*', 'COUNT(*) as playedMatches']); } elseif ($value == 'goal') { $sort = true; $sortColumn = 'goals'; $sel->join('Goal', 'id', 'playerId'); $sel->group('Player.id'); $sel->select(['Player.*', 'COUNT(*) as goals']); } elseif ($value == 'firstname') { $sort = true; $sortColumn = 'firstName'; } break; case 'order': $sortOrder = $value; break; case 'search': $value = str_replace('%', '\\%', $value); $value = str_replace('[', '\\[', $value); $value = str_replace(']', '\\]', $value); $value = str_replace('_', '\\_', $value); $value = explode(' ', $value); $filters = []; foreach ($value as $term) { $filters[] = ['Player.firstname', 'LIKE ', $term]; $filters[] = ['Player.lastname', 'LIKE ', $term]; } $sel->filter($filters); break; case 'limit': $limit = $value; break; } } if ($sort) { if ($sortOrder === '') { $sel->order($sortColumn, 'DESC'); } else { $sel->order($sortColumn, $sortOrder); } } $sel->limit(0, $limit); $result = $database->select($sel); $this->result = $database->resultToPlayers($result); }