diff --git a/lib/Doctrine/ORM/Tools/Pagination/CountWalker.php b/lib/Doctrine/ORM/Tools/Pagination/CountWalker.php index 64914f5a6..5294ab0a4 100644 --- a/lib/Doctrine/ORM/Tools/Pagination/CountWalker.php +++ b/lib/Doctrine/ORM/Tools/Pagination/CountWalker.php @@ -50,40 +50,24 @@ class CountWalker extends TreeWalkerAdapter throw new \RuntimeException('Cannot count query that uses a HAVING clause. Use the output walkers for pagination'); } - $rootComponents = array(); - foreach ($this->_getQueryComponents() as $dqlAlias => $qComp) { - $isParent = array_key_exists('parent', $qComp) - && $qComp['parent'] === null - && $qComp['nestingLevel'] == 0 - ; - if ($isParent) { - foreach($AST->fromClause->identificationVariableDeclarations as $identificationVariableDeclaration) { - $isRoot = $identificationVariableDeclaration->rangeVariableDeclaration - && $identificationVariableDeclaration->rangeVariableDeclaration->aliasIdentificationVariable == $dqlAlias - && $identificationVariableDeclaration->rangeVariableDeclaration->isRoot - ; - if ($isRoot) { - $rootComponents[] = array($dqlAlias => $qComp); - break; - } - } - } - } - if (count($rootComponents) > 1) { + $queryComponents = $this->_getQueryComponents(); + // Get the root entity and alias from the AST fromClause + $from = $AST->fromClause->identificationVariableDeclarations; + if (count($from) > 1) { throw new \RuntimeException("Cannot count query which selects two FROM components, cannot make distinction"); } - $root = reset($rootComponents); - $parentName = key($root); - $parent = current($root); - $identifierFieldName = $parent['metadata']->getSingleIdentifierFieldName(); + + $rootAlias = $from[0]->rangeVariableDeclaration->aliasIdentificationVariable; + $rootClass = $queryComponents[$rootAlias]['metadata']; + $identifierFieldName = $rootClass->getSingleIdentifierFieldName(); $pathType = PathExpression::TYPE_STATE_FIELD; - if (isset($parent['metadata']->associationMappings[$identifierFieldName])) { + if (isset($rootClass->associationMappings[$identifierFieldName])) { $pathType = PathExpression::TYPE_SINGLE_VALUED_ASSOCIATION; } $pathExpression = new PathExpression( - PathExpression::TYPE_STATE_FIELD | PathExpression::TYPE_SINGLE_VALUED_ASSOCIATION, $parentName, + PathExpression::TYPE_STATE_FIELD | PathExpression::TYPE_SINGLE_VALUED_ASSOCIATION, $rootAlias, $identifierFieldName ); $pathExpression->type = $pathType; diff --git a/lib/Doctrine/ORM/Tools/Pagination/LimitSubqueryWalker.php b/lib/Doctrine/ORM/Tools/Pagination/LimitSubqueryWalker.php index 2cf5e527f..c8cc4f7c0 100644 --- a/lib/Doctrine/ORM/Tools/Pagination/LimitSubqueryWalker.php +++ b/lib/Doctrine/ORM/Tools/Pagination/LimitSubqueryWalker.php @@ -60,37 +60,34 @@ class LimitSubqueryWalker extends TreeWalkerAdapter */ public function walkSelectStatement(SelectStatement $AST) { - $parent = null; - $parentName = null; + $queryComponents = $this->_getQueryComponents(); + // Get the root entity and alias from the AST fromClause + $from = $AST->fromClause->identificationVariableDeclarations; + $rootAlias = $from[0]->rangeVariableDeclaration->aliasIdentificationVariable; + $rootClass = $queryComponents[$rootAlias]['metadata']; $selectExpressions = array(); - foreach ($this->_getQueryComponents() as $dqlAlias => $qComp) { + foreach ($queryComponents as $dqlAlias => $qComp) { // Preserve mixed data in query for ordering. if (isset($qComp['resultVariable'])) { $selectExpressions[] = new SelectExpression($qComp['resultVariable'], $dqlAlias); continue; } - - if ($qComp['parent'] === null && $qComp['nestingLevel'] == 0) { - $parent = $qComp; - $parentName = $dqlAlias; - continue; - } } - - $identifier = $parent['metadata']->getSingleIdentifierFieldName(); - if (isset($parent['metadata']->associationMappings[$identifier])) { + + $identifier = $rootClass->getSingleIdentifierFieldName(); + if (isset($rootClass->associationMappings[$identifier])) { throw new \RuntimeException("Paginating an entity with foreign key as identifier only works when using the Output Walkers. Call Paginator#setUseOutputWalkers(true) before iterating the paginator."); } $this->_getQuery()->setHint( self::IDENTIFIER_TYPE, - Type::getType($parent['metadata']->getTypeOfField($identifier)) + Type::getType($rootClass->getTypeOfField($identifier)) ); $pathExpression = new PathExpression( PathExpression::TYPE_STATE_FIELD | PathExpression::TYPE_SINGLE_VALUED_ASSOCIATION, - $parentName, + $rootAlias, $identifier ); $pathExpression->type = PathExpression::TYPE_STATE_FIELD; diff --git a/lib/Doctrine/ORM/Tools/Pagination/WhereInWalker.php b/lib/Doctrine/ORM/Tools/Pagination/WhereInWalker.php index 460b8a708..936d6f103 100644 --- a/lib/Doctrine/ORM/Tools/Pagination/WhereInWalker.php +++ b/lib/Doctrine/ORM/Tools/Pagination/WhereInWalker.php @@ -71,39 +71,23 @@ class WhereInWalker extends TreeWalkerAdapter */ public function walkSelectStatement(SelectStatement $AST) { - $rootComponents = array(); - foreach ($this->_getQueryComponents() as $dqlAlias => $qComp) { - $isParent = array_key_exists('parent', $qComp) - && $qComp['parent'] === null - && $qComp['nestingLevel'] == 0 - ; - if ($isParent) { - foreach($AST->fromClause->identificationVariableDeclarations as $identificationVariableDeclaration) { - $isRoot = $identificationVariableDeclaration->rangeVariableDeclaration - && $identificationVariableDeclaration->rangeVariableDeclaration->aliasIdentificationVariable == $dqlAlias - && $identificationVariableDeclaration->rangeVariableDeclaration->isRoot - ; - if ($isRoot) { - $rootComponents[] = array($dqlAlias => $qComp); - break; - } - } - } - } - if (count($rootComponents) > 1) { + $queryComponents = $this->_getQueryComponents(); + // Get the root entity and alias from the AST fromClause + $from = $AST->fromClause->identificationVariableDeclarations; + if (count($from) > 1) { throw new \RuntimeException("Cannot count query which selects two FROM components, cannot make distinction"); } - $root = reset($rootComponents); - $parentName = key($root); - $parent = current($root); - $identifierFieldName = $parent['metadata']->getSingleIdentifierFieldName(); + + $rootAlias = $from[0]->rangeVariableDeclaration->aliasIdentificationVariable; + $rootClass = $queryComponents[$rootAlias]['metadata']; + $identifierFieldName = $rootClass->getSingleIdentifierFieldName(); $pathType = PathExpression::TYPE_STATE_FIELD; - if (isset($parent['metadata']->associationMappings[$identifierFieldName])) { + if (isset($rootClass->associationMappings[$identifierFieldName])) { $pathType = PathExpression::TYPE_SINGLE_VALUED_ASSOCIATION; } - $pathExpression = new PathExpression(PathExpression::TYPE_STATE_FIELD | PathExpression::TYPE_SINGLE_VALUED_ASSOCIATION, $parentName, $identifierFieldName); + $pathExpression = new PathExpression(PathExpression::TYPE_STATE_FIELD | PathExpression::TYPE_SINGLE_VALUED_ASSOCIATION, $rootAlias, $identifierFieldName); $pathExpression->type = $pathType; $count = $this->_getQuery()->getHint(self::HINT_PAGINATOR_ID_COUNT);