我的用户实体有几个关联
oneToMany:
images:
targetEntity: Image
mappedBy: user
posts:
targetEntity: Post
mappedBy: user
fetch: EXTRA_LAZY
postcomments:
targetEntity: Postcomment
mappedBy: user
fetch: EXTRA_LAZY
我想选择一个具有所有帖子和图像的特定用户,而不是他的帖子评论。这是我使用的查询:
$qb = $em->createQueryBuilder()
->select(array('u', 'p', 'i'))
->from('AppBundle:User', 'u')
->leftJoin('u.posts', 'p')
->leftJoin('u.images', 'i')
->where('u.id = :id')
->setParameter('id', $id);
然而,返回的结果也包含所有其他关联。就像我只选择u并删除连接一样。
如何选择结果中的特定关联?
下面是api调用的完整代码
public function getAction($id)
{
$em = $this->getDoctrine()->getEntityManager();
$qb = $em->createQueryBuilder()
->select(array('u', 'p', 'i'))
->from('AppBundle:User', 'u')
->leftJoin('u.posts', 'p')
->leftJoin('u.images', 'i')
->where('u.id = :id')
->setParameter('id', $id)
->getQuery();
$users = $db->getOneOrNullResult();
$view = $this->view($users);
return $this->handleView($view);
}`