Php 如何在条令查询生成器(Symfony)中使用countDistinct

Php 如何在条令查询生成器(Symfony)中使用countDistinct,php,symfony,doctrine-orm,query-builder,Php,Symfony,Doctrine Orm,Query Builder,我正在尝试计算为his查询返回的ID的不同数量: $query = $repo->createQueryBuilder('prov') ->select('c.id') ->innerJoin('prov.products', 'prod') ->innerJoin('prod.customerItems', 'ci') ->innerJoin('ci.customer', 'c') -

我正在尝试计算为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();
但我遇到了以下错误:

Attempted to call method "countDistinct" on class "Doctrine\ORM\QueryBuilder" [...]
我也试过了

$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();
这导致了这个错误:

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

我找不到任何其他的方法来说明如何不同于在您的情况下使用
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();

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();
应该有用。 或者简单地说:

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

谢谢,但是为什么DISTINCT与SUM一起使用时作为函数工作,而与COUNT一起使用时却不一样呢?有什么消息吗?