Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use countDistinct in Doctrine query builder (Symfony)

I am trying to count the distinct number of IDs returned for a query with his:

$query = $repo->createQueryBuilder('prov')
        ->select('c.id')
        ->innerJoin('prov.products', 'prod')
        ->innerJoin('prod.customerItems', 'ci')
        ->innerJoin('ci.customer', 'c')
        ->where('prov.id = :brand')
        ->setParameter('brand', $brand)
        ->countDistinct('c.id')
        ->getQuery();

I am getting this error though:

Attempted to call method "countDistinct" on class "Doctrine\ORM\QueryBuilder" [...]

I have also tried

$query = $repo->createQueryBuilder('prov')
        ->select('c.id')
        ->innerJoin('prov.products', 'prod')
        ->innerJoin('prod.customerItems', 'ci')
        ->innerJoin('ci.customer', 'c')
        ->where('prov.id = :brand')
        ->setParameter('brand', $brand)
        ->expr()->countDistinct('c.id')
        ->getQuery();

which leads to this error:

Error: Call to a member function getQuery() on a non-object in

I can't get any other pointers as to how to do this differently from the documentation

like image 841
matt_jay Avatar asked Aug 05 '14 11:08

matt_jay


Video Answer


2 Answers

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();
like image 85
Kamil Adryjanek Avatar answered Sep 18 '22 03:09

Kamil Adryjanek


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();
like image 43
Tomasz Madeyski Avatar answered Sep 18 '22 03:09

Tomasz Madeyski