怎么在Doctrine Query Builder(Symfony)中使用CountDist

原学程将引见若何在Doctrine Query Builder(Symfony)中应用CountDist的处置办法,这篇学程是从其余处所瞅到的,而后减了1些海外法式员的疑问与解问,愿望能对于您有所赞助,佳了,上面开端进修吧。

怎么在Doctrine Query Builder(Symfony)中使用CountDist 教程 第1张

成绩描写

我正在测验考试盘算应用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 "DoctrineORMQueryBuilder" [...]

我也试过

$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

我找没有就任何其余指导,解释怎样与documentation

分歧天履行此操纵

推举谜底

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

佳了闭于怎样在Doctrine Query Builder(Symfony)中应用CountDist的学程便到这里便停止了,愿望趣模板源码网找到的这篇技巧文章能赞助到年夜野,更多技巧学程不妨在站内搜刮。