I currently am working on a RESTful API with the PHP Slim Framework. I currently encountered the following problem:
I have to tables with an 1:n cardinality. With an GET-Call I want to get the data of the main table and nested in there all of the tables with the foreign key. Like this:
Planed Result
[{
"id":"1",
"ueberschrift":"UEBER",
"text_kurz":"Lorem ipsum...",
"kategorie":"HEAL",
"bilder":[
{"src":"wed1.png"},
{"src":"wed2.png"}
]},......
This is how a part of my mysql database looks like Current databse
Slim PHP Code
$app->get('/rezensionen', function ($request, $response, $args) {
$sth = $this->db->prepare("SELECT
rezension.id,ueberschrift,text_kurz,kategorie, bild.src FROM rezension INNER
JOIN bild ON (rezension.id=bild.rezension_id)");
$sth->execute();
$todos = $sth->fetchAll();
return $this->response->withJson($todos);
});
And this i what i get:
Actual Result
[
{
"id": "1",
"ueberschrift": "UEBER",
"text_kurz": "Lorem Ipsum...",
"kategorie": "HEAL",
"src": "wed1.png"
},
{
"id": "1",
"ueberschrift": "UEBER",
"text_kurz": "Lorem Ipsum...",
"kategorie": "HEAL",
"src": "wed2.png"
}
]
You hopefully know a proper way to achieve this!
Thanks a lot! Yours, David