Skip to content

Commit

Permalink
Add helper function TreeWalkerAdapter::getMetadataForDqlAlias() (#9932)
Browse files Browse the repository at this point in the history
  • Loading branch information
derrabus authored Jul 25, 2022
1 parent 5085dbe commit 7684cea
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 15 deletions.
14 changes: 14 additions & 0 deletions lib/Doctrine/ORM/Query/TreeWalkerAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}
}
9 changes: 3 additions & 6 deletions lib/Doctrine/ORM/Tools/Pagination/CountWalker.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use Doctrine\ORM\Query\TreeWalkerAdapter;
use RuntimeException;

use function assert;
use function count;
use function reset;

Expand All @@ -31,18 +30,16 @@ 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;

if (count($from) > 1) {
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;
Expand Down
6 changes: 2 additions & 4 deletions lib/Doctrine/ORM/Tools/Pagination/LimitSubqueryWalker.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use Doctrine\ORM\Query\TreeWalkerAdapter;
use RuntimeException;

use function assert;
use function count;
use function is_string;
use function reset;
Expand All @@ -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();
Expand Down Expand Up @@ -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(
Expand Down
8 changes: 3 additions & 5 deletions lib/Doctrine/ORM/Tools/Pagination/WhereInWalker.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,16 @@ 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;

if (count($from) > 1) {
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;
Expand Down

0 comments on commit 7684cea

Please sign in to comment.