我在帖子和评论之间有一对多的关系。
发布: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"
}
}
]