$qb = $this->createQueryBuilder('t');
return $qb
->join('t.customers', 'c')
->where($qb->expr()->eq('t.user', $user->getId()))
->andWhere($qb->expr()->gt($qb->expr()->count('c'), 0))
->orderBy('t.name')->getQuery()->getResult();
The above query (Doctrine2 generated one) is giving me this error:
#1111 - Invalid use of group function
but the strange thing is i'm not using GROUP BY. Any help is much appreciated, thanks.
SELECT t0_.id AS id0,
t0_.slug AS slug1,
t0_.name AS name2,
t0_.description AS description3,
t0_.user_id AS user_id4
FROM tag t0_
INNER JOIN customers_tags c2_ ON t0_.id = c2_.tag_id
INNER JOIN customer c1_ ON c1_.id = c2_.customer_id
WHERE t0_.user_id = 1 AND COUNT(c1_.id) > 0
ORDER BY t0_.name ASC
解决方案
You are using an aggregate function count() in the where clause which is not allowed.
Conditions on aggregate functions need to go into a HAVING clause
....
WHERE t0_.user_id = 1
HAVING count(c1_.id) > 0
And of course you'll need to use a GROUP BY to get correct results (although MySQL will probably let you get away with not using a GROUP BY - but then the results are unpredictable)