diff --git a/UPGRADE.md b/UPGRADE.md index 1a33d5b85af..303c37a6231 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -1,5 +1,27 @@ # Upgrade to 2.13 +## Deprecated omitting only the alias argument for `QueryBuilder::update` and `QueryBuilder::delete` + +When building an UPDATE or DELETE query and when passing a class/type to the function, the alias argument must not be omitted. + +### Before + +```php +$qb = $em->createQueryBuilder() + ->delete('User u') + ->where('u.id = :user_id') + ->setParameter('user_id', 1); +``` + +### After + +```php +$qb = $em->createQueryBuilder() + ->delete('User', 'u') + ->where('u.id = :user_id') + ->setParameter('user_id', 1); +``` + ## Deprecated using the `IDENTITY` identifier strategy on platform that do not support identity columns If identity columns are emulated with sequences on the platform you are using, diff --git a/lib/Doctrine/ORM/QueryBuilder.php b/lib/Doctrine/ORM/QueryBuilder.php index 3b7e5057614..a0504758a2b 100644 --- a/lib/Doctrine/ORM/QueryBuilder.php +++ b/lib/Doctrine/ORM/QueryBuilder.php @@ -6,6 +6,7 @@ use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Criteria; +use Doctrine\Deprecations\Deprecation; use Doctrine\ORM\Query\Expr; use Doctrine\ORM\Query\Parameter; use Doctrine\ORM\Query\QueryExpressionVisitor; @@ -844,6 +845,14 @@ public function delete($delete = null, $alias = null) return $this; } + if (! $alias) { + Deprecation::trigger( + 'doctrine/orm', + 'https://github.com/doctrine/orm/issues/9733', + 'Omitting the alias is deprecated and will throw an exception in Doctrine 3.0.' + ); + } + return $this->add('from', new Expr\From($delete, $alias)); } @@ -871,6 +880,14 @@ public function update($update = null, $alias = null) return $this; } + if (! $alias) { + Deprecation::trigger( + 'doctrine/orm', + 'https://github.com/doctrine/orm/issues/9733', + 'Omitting the alias is deprecated and will throw an exception in Doctrine 3.0.' + ); + } + return $this->add('from', new Expr\From($update, $alias)); } diff --git a/tests/Doctrine/Tests/ORM/QueryBuilderTest.php b/tests/Doctrine/Tests/ORM/QueryBuilderTest.php index 950ce55941d..ebbbb1e7e95 100644 --- a/tests/Doctrine/Tests/ORM/QueryBuilderTest.php +++ b/tests/Doctrine/Tests/ORM/QueryBuilderTest.php @@ -6,6 +6,7 @@ use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Criteria; +use Doctrine\Deprecations\PHPUnit\VerifyDeprecations; use Doctrine\ORM\Cache; use Doctrine\ORM\Query; use Doctrine\ORM\Query\Expr\Join; @@ -29,6 +30,8 @@ */ class QueryBuilderTest extends OrmTestCase { + use VerifyDeprecations; + /** @var EntityManagerMock */ private $entityManager; @@ -1279,4 +1282,25 @@ public function testJoin(): void self::assertSame('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u LEFT JOIN Doctrine\Tests\Models\CMS\CmsArticle a0 INNER JOIN Doctrine\Tests\Models\CMS\CmsArticle a1', $builder->getDQL()); } + + public function testUpdateDeprecationMessage(): void + { + $this->expectDeprecationWithIdentifier('https://github.com/doctrine/orm/issues/9733'); + + $qb = $this->entityManager->createQueryBuilder() + ->update(CmsUser::class . ' u') + ->set('u.username', ':username'); + + $this->assertValidQueryBuilder($qb, 'UPDATE Doctrine\Tests\Models\CMS\CmsUser u SET u.username = :username'); + } + + public function testDeleteDeprecationMessage(): void + { + $this->expectDeprecationWithIdentifier('https://github.com/doctrine/orm/issues/9733'); + + $qb = $this->entityManager->createQueryBuilder() + ->delete(CmsUser::class . ' u'); + + $this->assertValidQueryBuilder($qb, 'DELETE Doctrine\Tests\Models\CMS\CmsUser u '); + } }