How to use countDistinct in Doctrine query builder (Symfony)
countDistinct
is method of Expr class and COUNT DISTINCT need to be in SELECT statement so:
$qb = $repo->createQueryBuilder('prov');$query = $qb ->select($qb->expr()->countDistinct('c.id')) ->innerJoin('prov.products', 'prod') ->innerJoin('prod.customerItems', 'ci') ->innerJoin('ci.customer', 'c') ->where('prov.id = :brand') ->setParameter('brand', $brand) ->getQuery();
should work.Or simply:
$query = $repo->createQueryBuilder('prov') ->select('COUNT(DISTINCT c.id)') ->innerJoin('prov.products', 'prod') ->innerJoin('prod.customerItems', 'ci') ->innerJoin('ci.customer', 'c') ->where('prov.id = :brand') ->setParameter('brand', $brand) ->getQuery();
Proper way to use countDistinct
in your case is:
$qb = $repo->createQueryBuilder('prov');$query = $qb-> ->select($qb->expr()->countDistinct('c.id')) ->innerJoin('prov.products', 'prod') ->innerJoin('prod.customerItems', 'ci') ->innerJoin('ci.customer', 'c') ->where('prov.id = :brand') ->setParameter('brand', $brand) ->getQuery();