如何制作Doctrine多级API

时间:2016-11-04 15:43:03

标签: php symfony doctrine-orm doctrine

我正在尝试为我的应用制作一个API,并希望在一次获取中包含3个表。

这就是我现在的方式

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

我想获得这样的数据

{
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
},

这是我获取数据的代码

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;
}

在学说中有没有一种简单的方法可以做到这一点?

1 个答案:

答案 0 :(得分:0)

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;
}