I have two entities: Cage
and Bird
. Cage
has Birds
inside so their relationship is one-to-many.
Bird
has a field name
. How can I select all Cages
where there's no Bird
with name eagle
inside.
I was trying to do this:
$cages = $this->createQueryBuilder("c")
->leftJoin("c.birds", "b")
->where("b.name != :name")
->setParameter("name", 'eagle')
->getQuery()->getResult();
This works if there's only single Bird
(eagle
) in the Cage
. Then the Cage
is not selected which is correct behavior.
But if there are multiple Birds
and one of them is eagle
, the Cage
gets selected even though the eagle
is inside.
This is the idea, adapt table and column names as necessary:
So, using doctrine:
(Inspired by 'where not in' query with doctrine query builder)