Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add helper function TreeWalkerAdapter::getMetadataForDqlAlias() #9932

Merged
merged 1 commit into from
Jul 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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