0

I have two entities in Doctrine: oneToMany between Post and Comments. Comments are owned by a User. I want to retrieve all posts and comments for which at least one comment is owned by a certain user. What would be the proper way of achieving that with Doctrine's Query Builder?

$em->createQueryBuilder('p')
  ->innerJoin('p.comments','c')
  ->having(AT LEAST ON c.user = :user)
  ->where(p.id = :idPost)

Could you help me folks ?

hasumedic
  • 2,139
  • 12
  • 17
user2626210
  • 115
  • 5
  • 13

1 Answers1

1

Here is a solution:

$query = $em->createQueryBuilder('p')
  ->leftJoin('p.comments','c')
  ->where('c.user = :user')
  ->andwhere('p.id = :idPost')
  ->setParameter('idPost', '1')
  ->setParameter('user', 'Toto')
;

return $query->getQuery()->getResult();
scoolnico
  • 3,055
  • 14
  • 24
  • Hello, that's not the point. If I set user = toto, I will have only the message where owner is toto, but i want all the message connected to my post where at least one is owned by toto ... – user2626210 Feb 27 '16 at 07:22