Nico Shultz Nico Shultz - 22 days ago 9
PHP Question

how to make a Doctrine multi level API

I am trying to make a API for my app and I want to include 3 tables in one get.

This is how I get it now

{
id: 1,
tocht_id: 1,
user_id: 1,
started_bool: true,
finished_bool: false
},


I want to get the data like this

{
id: 1,
tocht {
id: 1
naam: intro tocht
opleiding: testOpleiding
informatie: testInfo }
user {
id: 1
naam: vincent
pin: 666 }
started_bool: true,
finished_bool: false
},


This is my code to get the data

public function getAction()
{
$restresult = $this->getDoctrine()->getRepository('AppBundle:Koppel_tocht_user')->findAll();
if ($restresult === null)
{
return new View("there are no records to display.", Response::HTTP_NOT_FOUND);
}
return $restresult;
}


Is there an easy way to do this in doctrine?

Answer
public function getAction()
{
  $restresult = $this->getDoctrine()->getRepository('AppBundle:Koppel_tocht_user')->findAll();
  //var_dump($restresult);

  foreach ($restresult as $value) {
    $questId = $value->getTochtId();
    $userId = $value->getUserId();
    $questResult = $this->getDoctrine()->getRepository('AppBundle:Speurtocht')->findById($questId);
    $userResult = $this->getDoctrine()->getRepository('AppBundle:User')->findById($userId);
    $value->setQuest($questResult);
    $value->setUser($userResult);
  }

    if ($restresult === null) 
    {
      return new View("there are no records to display.", Response::HTTP_NOT_FOUND);
    }
    return $restresult;
}