2
votes

I'm having a problem with counting some aggregations.

Part of code looks like this:

$qb->select($qb->expr()->countDistinct('policy.calculation', 'policy.id'))
    ->getQuery()
    ->getSingleScalarResult();

In sql it's possible to count distinct on multiple columns, and from DQL I'm getting an error:

[Syntax Error] line 0, col 40: Error: Expected Doctrine\ORM\Query\Lexer::T_CLOSE_PARENTHESIS, got ','

Can I get this working?

From expression builder:

/**
 * Creates an instance of COUNT(DISTINCT) function, with the given argument.
 *
 * @param mixed $x Argument to be used in COUNT(DISTINCT) function.
 *
 * @return string
 */
public function countDistinct($x)
{
    return 'COUNT(DISTINCT ' . implode(', ', func_get_args()) . ')';
}

It's looks like this should work.

1
Found issue related: github.com/doctrine/doctrine2/issues/5544 2 years ago, not solved.Oskar Gunther

1 Answers

0
votes

for future cases

before

public function countDistinct($x)
{
    return 'COUNT(DISTINCT ' . implode(', ', func_get_args()) . ')';
}

after

public function countDistinct($x)
{
     return 'COUNT(DISTINCT CONCAT(' . implode(', ', func_get_args()) . '))';
}