Skip to content

Commit 5301b99

Browse files
authored
Merge pull request #12051 from greg0ire/stop-using-depr-method
Stop using QueryBuilder::getRootAlias()
2 parents 6deec36 + 00c7b70 commit 5301b99

File tree

3 files changed

+35
-6
lines changed

3 files changed

+35
-6
lines changed

UPGRADE.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# Upgrade to 3.6
2+
3+
Using `Doctrine\ORM\QueryBuilder::add('join', ...)` with a list of join parts
4+
is deprecated in favor of using an associative array of join parts with the
5+
root alias as key.
6+
17
# Upgrade to 3.5
28

39
## Deprecate not using native lazy objects on PHP 8.4+

src/QueryBuilder.php

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Doctrine\Common\Collections\Criteria;
99
use Doctrine\DBAL\ArrayParameterType;
1010
use Doctrine\DBAL\ParameterType;
11+
use Doctrine\Deprecations\Deprecation;
1112
use Doctrine\ORM\Internal\NoUnknownNamedArguments;
1213
use Doctrine\ORM\Query\Expr;
1314
use Doctrine\ORM\Query\Parameter;
@@ -305,8 +306,13 @@ private function findRootAlias(string $alias, string $parentAlias): string
305306
} else {
306307
// Should never happen with correct joining order. Might be
307308
// thoughtful to throw exception instead.
308-
// @phpstan-ignore method.deprecated
309-
$rootAlias = $this->getRootAlias();
309+
$aliases = $this->getRootAliases();
310+
311+
if (! isset($aliases[0])) {
312+
throw new RuntimeException('No alias was set before invoking getRootAlias().');
313+
}
314+
315+
$rootAlias = $aliases[0];
310316
}
311317

312318
$this->joinRootAliases[$alias] = $rootAlias;
@@ -582,14 +588,25 @@ public function add(string $dqlPartName, string|object|array $dqlPart, bool $app
582588
$dqlPart = reset($dqlPart);
583589
}
584590

585-
// This is introduced for backwards compatibility reasons.
586-
// TODO: Remove for 3.0
587591
if ($dqlPartName === 'join') {
588592
$newDqlPart = [];
589593

590594
foreach ($dqlPart as $k => $v) {
591-
// @phpstan-ignore method.deprecated
592-
$k = is_numeric($k) ? $this->getRootAlias() : $k;
595+
if (is_numeric($k)) {
596+
Deprecation::trigger(
597+
'doctrine/orm',
598+
'https://github.com/doctrine/orm/pull/12051',
599+
'Using numeric keys in %s for join parts is deprecated and will not be supported in 4.0. Use an associative array with the root alias as key instead.',
600+
__METHOD__,
601+
);
602+
$aliases = $this->getRootAliases();
603+
604+
if (! isset($aliases[0])) {
605+
throw new RuntimeException('No alias was set before invoking add().');
606+
}
607+
608+
$k = $aliases[0];
609+
}
593610

594611
$newDqlPart[$k] = $v;
595612
}

tests/Tests/ORM/QueryBuilderTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Doctrine\Common\Collections\Criteria;
1010
use Doctrine\Common\Collections\Order;
1111
use Doctrine\DBAL\Types\Types;
12+
use Doctrine\Deprecations\PHPUnit\VerifyDeprecations;
1213
use Doctrine\ORM\Cache;
1314
use Doctrine\ORM\Query;
1415
use Doctrine\ORM\Query\Expr\Join;
@@ -24,6 +25,7 @@
2425
use Doctrine\Tests\OrmTestCase;
2526
use InvalidArgumentException;
2627
use PHPUnit\Framework\Attributes\Group;
28+
use PHPUnit\Framework\Attributes\WithoutErrorHandler;
2729
use PHPUnit\Framework\TestCase;
2830

2931
use function array_filter;
@@ -35,6 +37,8 @@
3537
*/
3638
class QueryBuilderTest extends OrmTestCase
3739
{
40+
use VerifyDeprecations;
41+
3842
private EntityManagerMock $entityManager;
3943

4044
protected function setUp(): void
@@ -1031,8 +1035,10 @@ public function testGetSeveralRootAliases(): void
10311035
self::assertEquals('u', $qb->getRootAlias());
10321036
}
10331037

1038+
#[WithoutErrorHandler]
10341039
public function testBCAddJoinWithoutRootAlias(): void
10351040
{
1041+
$this->expectDeprecationWithIdentifier('https://github.com/doctrine/orm/pull/12051');
10361042
$qb = $this->entityManager->createQueryBuilder()
10371043
->select('u')
10381044
->from(CmsUser::class, 'u')

0 commit comments

Comments
 (0)