I have two classes in this example - DeliveryMethod and Country. They have a many-to-many relationship with each other.
What I want to do is select all DeliveryMetho
is empty
It's specifically designed to check for empty associations:
$qb->select('m')->from('DeliveryMethods', 'm')->where('m.countries is empty')
See: Doctrine 2 ORM Documentation: Doctrine Query Language (search for "is empty")
There is no need in joins and havings. Simply use SIZE
function:
$qb->select('m')
->from('DeliveryMethods','m')
->where('SIZE(m.countries) = 0');
This will give you all methods without attached countries
Can't join NULL values, IIRC. Apologies for the typo on twitter, should have said "can't".
What about this? Assuming $qb
is your query builder instance
$qb->select('m')
->from('DeliveryMethods','m')
->leftJoin('m.countries','c')
->having('COUNT(c.id) = 0')
->groupBy('m.id');
This would give you the DeliveryMethods which is associated with countries and count of the associated countries is 0