I have implemented the following function, which is very close to what I actually want:
public function loadUserGroupsOfNewsletter($ids, $newsletterID)
{
$qb = $this->getEntityManager()->createQueryBuilder();
return $qb->select('ug, gon')
->from('MyNameSpace:UserGroup', 'ug')
->leftJoin('ug.groupsOfNewsletter', 'gon')
->leftJoin('gon.newsletterItem', 'n')
->where($qb->expr()->in('ug.id', $ids))
->where('n.id = :id')
->setParameter('id', $newsletterID)
->getQuery()->getResult();
}
I would like to get at least a record for each user group. If there is no newsletter matching the id for a newsletter, then I would like gon
to be null
for the given result item, but if there is at least a newsletter matching the id, then I would like to have the correct user group and gon record in the result item.
So,
Case 1. The given user group does not have a matching newsletter:
The result item is expected to contain the correct ug
and null
as gon
Case 2. The given user group has at least a matching newsletter:
There are as many newsletter items as many such matches exist and all result items for this user group will contain valid ug
and gon
.
I have tried to check n.id against null and the results matched my expectation, so I assume that the following query achieves what I intended, due to the fact that my experiments yielded good results. Am I right in this assumption?
public function loadUserGroupsOfNewsletter($ids, $newsletterID)
{
$qb = $this->getEntityManager()->createQueryBuilder();
return $qb->select('ug, gon')
->from('MyNameSpace:UserGroup', 'ug')
->leftJoin('ug.groupsOfNewsletter', 'gon')
->leftJoin('gon.newsletterItem', 'n')
->where($qb->expr()->in('ug.id', $ids))
->where('(n.id is null) or (n.id = :id)')
->setParameter('id', $newsletterID)
->getQuery()->getResult();
}