0
votes

I have two entities Message and Skill, and many to many relationship between them. I am trying to get all messages with skill=5 or without any skills.

$queryBuilder = $repository->createQueryBuilder('entity');
$queryBuilder->leftJoin('entity.skills', 'skill');
$queryBuilder->andWhere(
    $criteria->expr()->orX(
        $criteria->expr()->eq('skill.id', 5),
        $criteria->expr()->isNull('skill.id')
  )
)

But in results I have question ONLY with skill=5. How can I select questions without skill or with skill=5 is SINGLE query?

1
Did you try expanding your leftJoin(): $queryBuilder->leftJoin('entity.skills', 'skill', 'WITH', 'skill.id = 5 OR skill.id IS NULL'); (and getting rid if the where calls in that case)? - ccKep

1 Answers

1
votes
$queryBuilder = $repository->createQueryBuilder('entity');
$queryBuilder->leftJoin('entity.skills', 'skill');
$queryBuilder->where('skill.id = 5');
$queryBuilder->orWhere('skill.id IS NULL');