doctrine/orm · error · RuntimeException

Cannot count query that uses a HAVING clause. Use the output

Error message

Cannot count query that uses a HAVING clause. Use the output walkers for pagination

What it means

Error "Cannot count query that uses a HAVING clause. Use the output walkers for pagination" thrown in doctrine/orm.

Source

Thrown at src/Tools/Pagination/CountWalker.php:30

use RuntimeException;

use function count;
use function reset;

/**
 * Replaces the selectClause of the AST with a COUNT statement.
 */
class CountWalker extends TreeWalkerAdapter
{
    /**
     * Distinct mode hint name.
     */
    public const HINT_DISTINCT = 'doctrine_paginator.distinct';

    public function walkSelectStatement(SelectStatement $selectStatement): void
    {
        if ($selectStatement->havingClause) {
            throw new RuntimeException('Cannot count query that uses a HAVING clause. Use the output walkers for pagination');
        }

        // Get the root entity and alias from the AST fromClause
        $from = $selectStatement->fromClause->identificationVariableDeclarations;

        if (count($from) > 1) {
            throw new RuntimeException('Cannot count query which selects two FROM components, cannot make distinction');
        }

        $distinct = $this->_getQuery()->getHint(self::HINT_DISTINCT);

        $countPathExpressionOrLiteral = '*';
        if ($distinct) {
            $fromRoot            = reset($from);
            $rootAlias           = $fromRoot->rangeVariableDeclaration->aliasIdentificationVariable;
            $rootClass           = $this->getMetadataForDqlAlias($rootAlias);
            $identifierFieldName = $rootClass->getSingleIdentifierFieldName();

View on GitHub (pinned to d9b9ff7301)

When it happens

Trigger: Thrown at src/Tools/Pagination/CountWalker.php:30 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of doctrine/orm@d9b9ff7301 (2026-08-21). Data as JSON: /api/errors/44322e2d53827124. Report an issue: GitHub.