Symfony(Doctrine) - leftJoin,OneToMany和结果层次结构

时间:2017-09-15 09:12:46

标签: php symfony doctrine-orm

我在帖子和评论之间有一对多的关系。

发布:id,title

评论:id,title,text,post_id

$qb = $this->entityManager->getRepository('MyBundle:Comment')->createQueryBuilder('comment');

$qb->select('comment, post')->leftJoin('comment.post', 'post');

$result = $qb->getQuery()->getArrayResult();

return new JsonResponse($result);

我明白了:

[
  {
    "id": 1,
    "title": "Comment 1",
    "text": "Comment text 1",
    "post": {
      "id": 1,
      "title": "Post 1"
    }
  }
]

这就是我想要的:

[
  {
    "id": 1,
    "title": "Post 1",
    "comment": {
      "id": 1,
      "title": "Comment 1",
      "text": "Comment text 1"
    }
  }
]

0 个答案:

没有答案