Skip to content

Commit

Permalink
Deprecate omitting the alias in QueryBuilder (#9765)
Browse files Browse the repository at this point in the history
… methods update() and delete()

Co-authored-by: Alexander M. Turek <me@derrabus.de>
  • Loading branch information
Hanmac and derrabus committed Jun 17, 2022
1 parent d15eef9 commit b931a59
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
22 changes: 22 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
17 changes: 17 additions & 0 deletions lib/Doctrine/ORM/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}

Expand Down Expand Up @@ -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));
}

Expand Down
24 changes: 24 additions & 0 deletions tests/Doctrine/Tests/ORM/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -29,6 +30,8 @@
*/
class QueryBuilderTest extends OrmTestCase
{
use VerifyDeprecations;

/** @var EntityManagerMock */
private $entityManager;

Expand Down Expand Up @@ -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 ');
}
}

0 comments on commit b931a59

Please sign in to comment.