Search code examples
doctrine-ormdql

Doctrine2: Filtering by ManToMany Association


I want to retrieve a collection of objects based on what they are associated to. For example, by a category. This would be a Many to Many relationship.

I've been able to achieve that with MEMBER OF, however I need to pass in an array of IDs, opposed to one at a time. I see there is an "IN ()", but it seems to require a subquery, which I would like to avoid.

MEMBER OF example: SELECT o FROM Entity\Object1 o WHERE 'CATEGORY_CODE' MEMBER OF o.categories

(Edit) This is what I would like to do, but perhaps I'm misunderstanding how entities work in DQL: SELECT o FROM Entity\Object1 o WHERE o.categories.Id IN (id, id, id)


Solution

  •  SELECT o FROM Entity\Object1 o JOIN o.categories c WHERE c.id in ('id', 'id', 'id');
    

    If this isn't what you want, you'll have to edit your question to be more specific.