doctrine/orm · error · RuntimeException

Cannot count query which selects two FROM components, cannot

Error message

Cannot count query which selects two FROM components, cannot make distinction

What it means

Error "Cannot count query which selects two FROM components, cannot make distinction" thrown in doctrine/orm.

Source

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

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

            $pathType = PathExpression::TYPE_STATE_FIELD;
            if (isset($rootClass->associationMappings[$identifierFieldName])) {
                $pathType = PathExpression::TYPE_SINGLE_VALUED_ASSOCIATION;
            }

            $countPathExpressionOrLiteral       = new PathExpression(
                PathExpression::TYPE_STATE_FIELD | PathExpression::TYPE_SINGLE_VALUED_ASSOCIATION,

View on GitHub (pinned to d9b9ff7301)

When it happens

Trigger: Thrown at src/Tools/Pagination/CountWalker.php:37 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/b964c8b34a9f4160. Report an issue: GitHub.