From f56f8196e05a6f248faa89136f5d0248a33ac91b Mon Sep 17 00:00:00 2001 From: "Alexander M. Turek" Date: Mon, 25 Jul 2022 19:20:17 +0200 Subject: [PATCH] Add helper function TreeWalkerAdapter::getMetadataForDqlAlias() --- lib/Doctrine/ORM/Query/TreeWalkerAdapter.php | 14 ++++++++++++++ lib/Doctrine/ORM/Tools/Pagination/CountWalker.php | 9 +++------ .../ORM/Tools/Pagination/LimitSubqueryWalker.php | 6 ++---- .../ORM/Tools/Pagination/WhereInWalker.php | 8 +++----- 4 files changed, 22 insertions(+), 15 deletions(-) diff --git a/lib/Doctrine/ORM/Query/TreeWalkerAdapter.php b/lib/Doctrine/ORM/Query/TreeWalkerAdapter.php index 5f48f594c0b..9a46e554e6d 100644 --- a/lib/Doctrine/ORM/Query/TreeWalkerAdapter.php +++ b/lib/Doctrine/ORM/Query/TreeWalkerAdapter.php @@ -6,11 +6,14 @@ use Doctrine\Deprecations\Deprecation; use Doctrine\ORM\AbstractQuery; +use Doctrine\ORM\Mapping\ClassMetadata; +use LogicException; use function array_diff; use function array_keys; use function debug_backtrace; use function is_a; +use function sprintf; use const DEBUG_BACKTRACE_IGNORE_ARGS; @@ -800,4 +803,15 @@ public function getExecutor($AST) return null; } + + final protected function getMetadataForDqlAlias(string $dqlAlias): ClassMetadata + { + $metadata = $this->_getQueryComponents()[$dqlAlias]['metadata'] ?? null; + + if ($metadata === null) { + throw new LogicException(sprintf('No metadata for DQL alias: %s', $dqlAlias)); + } + + return $metadata; + } } diff --git a/lib/Doctrine/ORM/Tools/Pagination/CountWalker.php b/lib/Doctrine/ORM/Tools/Pagination/CountWalker.php index 3de87da42ca..12c5efe32fb 100644 --- a/lib/Doctrine/ORM/Tools/Pagination/CountWalker.php +++ b/lib/Doctrine/ORM/Tools/Pagination/CountWalker.php @@ -11,7 +11,6 @@ use Doctrine\ORM\Query\TreeWalkerAdapter; use RuntimeException; -use function assert; use function count; use function reset; @@ -31,7 +30,6 @@ public function walkSelectStatement(SelectStatement $AST) throw new RuntimeException('Cannot count query that uses a HAVING clause. Use the output walkers for pagination'); } - $queryComponents = $this->_getQueryComponents(); // Get the root entity and alias from the AST fromClause $from = $AST->fromClause->identificationVariableDeclarations; @@ -39,10 +37,9 @@ public function walkSelectStatement(SelectStatement $AST) throw new RuntimeException('Cannot count query which selects two FROM components, cannot make distinction'); } - $fromRoot = reset($from); - $rootAlias = $fromRoot->rangeVariableDeclaration->aliasIdentificationVariable; - assert(isset($queryComponents[$rootAlias]['metadata'])); - $rootClass = $queryComponents[$rootAlias]['metadata']; + $fromRoot = reset($from); + $rootAlias = $fromRoot->rangeVariableDeclaration->aliasIdentificationVariable; + $rootClass = $this->getMetadataForDqlAlias($rootAlias); $identifierFieldName = $rootClass->getSingleIdentifierFieldName(); $pathType = PathExpression::TYPE_STATE_FIELD; diff --git a/lib/Doctrine/ORM/Tools/Pagination/LimitSubqueryWalker.php b/lib/Doctrine/ORM/Tools/Pagination/LimitSubqueryWalker.php index 0070e077d53..e0531fddac5 100644 --- a/lib/Doctrine/ORM/Tools/Pagination/LimitSubqueryWalker.php +++ b/lib/Doctrine/ORM/Tools/Pagination/LimitSubqueryWalker.php @@ -15,7 +15,6 @@ use Doctrine\ORM\Query\TreeWalkerAdapter; use RuntimeException; -use function assert; use function count; use function is_string; use function reset; @@ -38,13 +37,11 @@ class LimitSubqueryWalker extends TreeWalkerAdapter public function walkSelectStatement(SelectStatement $AST) { - $queryComponents = $this->_getQueryComponents(); // Get the root entity and alias from the AST fromClause $from = $AST->fromClause->identificationVariableDeclarations; $fromRoot = reset($from); $rootAlias = $fromRoot->rangeVariableDeclaration->aliasIdentificationVariable; - assert(isset($queryComponents[$rootAlias]['metadata'])); - $rootClass = $queryComponents[$rootAlias]['metadata']; + $rootClass = $this->getMetadataForDqlAlias($rootAlias); $this->validate($AST); $identifier = $rootClass->getSingleIdentifierFieldName(); @@ -75,6 +72,7 @@ public function walkSelectStatement(SelectStatement $AST) return; } + $queryComponents = $this->_getQueryComponents(); foreach ($AST->orderByClause->orderByItems as $item) { if ($item->expression instanceof PathExpression) { $AST->selectClause->selectExpressions[] = new SelectExpression( diff --git a/lib/Doctrine/ORM/Tools/Pagination/WhereInWalker.php b/lib/Doctrine/ORM/Tools/Pagination/WhereInWalker.php index aa346094360..e3d12bff870 100644 --- a/lib/Doctrine/ORM/Tools/Pagination/WhereInWalker.php +++ b/lib/Doctrine/ORM/Tools/Pagination/WhereInWalker.php @@ -49,7 +49,6 @@ class WhereInWalker extends TreeWalkerAdapter public function walkSelectStatement(SelectStatement $AST) { - $queryComponents = $this->_getQueryComponents(); // Get the root entity and alias from the AST fromClause $from = $AST->fromClause->identificationVariableDeclarations; @@ -57,10 +56,9 @@ public function walkSelectStatement(SelectStatement $AST) throw new RuntimeException('Cannot count query which selects two FROM components, cannot make distinction'); } - $fromRoot = reset($from); - $rootAlias = $fromRoot->rangeVariableDeclaration->aliasIdentificationVariable; - assert(isset($queryComponents[$rootAlias]['metadata'])); - $rootClass = $queryComponents[$rootAlias]['metadata']; + $fromRoot = reset($from); + $rootAlias = $fromRoot->rangeVariableDeclaration->aliasIdentificationVariable; + $rootClass = $this->getMetadataForDqlAlias($rootAlias); $identifierFieldName = $rootClass->getSingleIdentifierFieldName(); $pathType = PathExpression::TYPE_STATE_FIELD;