Did you check that? http://www.philipphoffmann.de/blog/2012/08/29/a-bulletproof-pattern-for-creating-doctrine-subqueries-of-any-complexity/