I'm using Slim
for write a little rest api, and I have the following situation: I need to return a list of competitions which have different seasons available, now if I do this:
$app->get('/get_competitions', function (Request $request, Response $response, array $args)
{
$sql = $this->db->query("SELECT * FROM competition
INNER JOIN competition_seasons ON competition.id = competition_seasons.competition_id
WHERE country_id IS NOT NULL");
$sql->execute();
$comps = $sql->fetchAll();
return $response->withJson($comps);
});
this will return the following response:
[
{
"id": "1",
"country_id": "1",
"name": "2017",
"link": "https:\/\/int.soccerway.com\/national\/afghanistan\/afghan-premier-league\/c1093\/",
"competition_id": "1093",
"season_id": "13198",
"update_at": "2018-05-14 12:51:17"
},
{
"id": "2",
"country_id": "1",
"name": "2018",
"link": "https:\/\/int.soccerway.com\/national\/afghanistan\/afghan-premier-league\/c1093\/",
"competition_id": "1093",
"season_id": "14963",
"update_at": "2018-05-16 12:10:16"
},
{
"id": "5",
"country_id": "1",
"name": "2016",
"link": "https:\/\/int.soccerway.com\/national\/afghanistan\/afghan-premier-league\/c1093\/",
"competition_id": "1093",
"season_id": "12091",
"update_at": "2018-05-14 12:52:47"
},
{
"id": "6",
"country_id": "1",
"name": "2015",
"link": "https:\/\/int.soccerway.com\/national\/afghanistan\/afghan-premier-league\/c1093\/",
"competition_id": "1093",
"season_id": "10891",
"update_at": "2018-05-14 12:54:12"
},
{
"id": "7",
"country_id": "1",
"name": "2014",
"link": "https:\/\/int.soccerway.com\/national\/afghanistan\/afghan-premier-league\/c1093\/",
"competition_id": "1093",
"season_id": "8868",
"update_at": "2018-05-14 12:55:40"
},
{
"id": "8",
"country_id": "1",
"name": "2013",
"link": "https:\/\/int.soccerway.com\/national\/afghanistan\/afghan-premier-league\/c1093\/",
"competition_id": "1093",
"season_id": "7851",
"update_at": "2018-05-14 12:57:02"
},
{
"id": "9",
"country_id": "1",
"name": "2012",
"link": "https:\/\/int.soccerway.com\/national\/afghanistan\/afghan-premier-league\/c1093\/",
"competition_id": "1093",
"season_id": "7816",
"update_at": "2018-05-14 12:58:34"
},
{
"id": "10",
"country_id": "2",
"name": "2017\/2018",
"link": "https:\/\/int.soccerway.com\/national\/albania\/super-league\/c48\/",
"competition_id": "48",
"season_id": "14214",
"update_at": "2018-05-16 12:11:12"
}
]
as you can see the competition available have the id 1093
and different seasons, how can I optimize the query (if is possible), to return a response which contains one competition with a list of seasons?
Something like:
[
{
"id":"1",
"country_id":"1",
"name":"2017",
"link":"https:\/\/int.soccerway.com\/national\/afghanistan\/afghan-premier-league\/c1093\/",
"competition_id":"1093",
"seasons":[
"season_id":13198,
"season_id":12091,
etc..
],
"update_at":"2018-05-14 12:51:17"
}
]
Thanks.