如何在Doctrine查询生成器(Symfony)中使用countDistinct

时间:2022-11-01 20:08:58

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

我试图计算为他的查询返回的不同ID数:

$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.

关于如何以不同的方式执行此操作,我无法获得任何其他指示。

Can anyone suggest a solution?enter link description here

有人可以建议解决方案吗?在这里输入链接描述

2 个解决方案

#1


18  

countDistinct is method of Expr class and COUNT DISTINCT need to be in SELECT statement so:

countDistinct是Expr类的方法,COUNT DISTINCT需要在SELECT语句中,所以:

$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();

#2


2  

Proper way to use countDistinct in your case is:

在您的情况下使用countDistinct的正确方法是:

$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();

#1


18  

countDistinct is method of Expr class and COUNT DISTINCT need to be in SELECT statement so:

countDistinct是Expr类的方法,COUNT DISTINCT需要在SELECT语句中,所以:

$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();

#2


2  

Proper way to use countDistinct in your case is:

在您的情况下使用countDistinct的正确方法是:

$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();