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

Merge 2.14.x into 3.0.x #10195

Merged
merged 3 commits into from
Nov 2, 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
6 changes: 5 additions & 1 deletion docs/en/reference/transactions-and-concurrency.rst
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ Doctrine ORM currently supports two pessimistic lock modes:
locks other concurrent requests that attempt to update or lock rows
in write mode.

You can use pessimistic locks in three different scenarios:
You can use pessimistic locks in four different scenarios:


1. Using
Expand All @@ -384,6 +384,10 @@ You can use pessimistic locks in three different scenarios:
or
``EntityManager#lock($entity, \Doctrine\DBAL\LockMode::PESSIMISTIC_READ)``
3. Using
``EntityManager#refresh($entity, \Doctrine\DBAL\LockMode::PESSIMISTIC_WRITE)``
or
``EntityManager#refresh($entity, \Doctrine\DBAL\LockMode::PESSIMISTIC_READ)``
4. Using
``Query#setLockMode(\Doctrine\DBAL\LockMode::PESSIMISTIC_WRITE)``
or
``Query#setLockMode(\Doctrine\DBAL\LockMode::PESSIMISTIC_READ)``
14 changes: 14 additions & 0 deletions lib/Doctrine/ORM/Decorator/EntityManagerDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
use Doctrine\ORM\UnitOfWork;
use Doctrine\Persistence\ObjectManagerDecorator;

use function func_get_arg;
use function func_num_args;

/**
* Base class for EntityManager decorators
*
Expand Down Expand Up @@ -130,6 +133,17 @@ public function find(string $className, mixed $id, LockMode|int|null $lockMode =
return $this->wrapped->find($className, $id, $lockMode, $lockVersion);
}

public function refresh(object $object): void
{
$lockMode = null;

if (func_num_args() > 1) {
$lockMode = func_get_arg(1);
}

$this->wrapped->refresh($object, $lockMode);
}

public function getEventManager(): EventManager
{
return $this->wrapped->getEventManager();
Expand Down
7 changes: 5 additions & 2 deletions lib/Doctrine/ORM/EntityManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -500,14 +500,17 @@ public function remove(object $object): void
* Refreshes the persistent state of an entity from the database,
* overriding any local changes that have not yet been persisted.
*
* @psalm-param LockMode::*|null $lockMode
*
* @throws ORMInvalidArgumentException
* @throws ORMException
* @throws TransactionRequiredException
*/
public function refresh(object $object): void
public function refresh(object $object, LockMode|int|null $lockMode = null): void
{
$this->errorIfClosed();

$this->unitOfWork->refresh($object);
$this->unitOfWork->refresh($object, $lockMode);
}

/**
Expand Down
1 change: 1 addition & 0 deletions lib/Doctrine/ORM/EntityManagerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Doctrine\ORM\Query\ResultSetMapping;
use Doctrine\Persistence\ObjectManager;

/** @method void refresh(object $object, LockMode|int|null $lockMode = null) */
interface EntityManagerInterface extends ObjectManager
{
/**
Expand Down
33 changes: 27 additions & 6 deletions lib/Doctrine/ORM/UnitOfWork.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@
use function assert;
use function count;
use function current;
use function func_get_arg;
use function func_num_args;
use function get_debug_type;
use function implode;
use function in_array;
Expand Down Expand Up @@ -1789,23 +1791,40 @@ private function doDetach(
* any local, unpersisted changes.
*
* @throws InvalidArgumentException If the entity is not MANAGED.
* @throws TransactionRequiredException
*/
public function refresh(object $entity): void
{
$visited = [];

$this->doRefresh($entity, $visited);
$lockMode = null;

if (func_num_args() > 1) {
$lockMode = func_get_arg(1);
}

$this->doRefresh($entity, $visited, $lockMode);
}

/**
* Executes a refresh operation on an entity.
*
* @psalm-param array<int, object> $visited The already visited entities during cascades.
* @psalm-param LockMode::*|null $lockMode
*
* @throws ORMInvalidArgumentException If the entity is not MANAGED.
* @throws TransactionRequiredException
*/
private function doRefresh(object $entity, array &$visited): void
private function doRefresh(object $entity, array &$visited, LockMode|int|null $lockMode = null): void
{
switch (true) {
case $lockMode === LockMode::PESSIMISTIC_READ:
case $lockMode === LockMode::PESSIMISTIC_WRITE:
if (! $this->em->getConnection()->isTransactionActive()) {
throw TransactionRequiredException::transactionRequired();
}
}

$oid = spl_object_id($entity);

if (isset($visited[$oid])) {
Expand All @@ -1823,17 +1842,19 @@ private function doRefresh(object $entity, array &$visited): void
$this->getEntityPersister($class->name)->refresh(
array_combine($class->getIdentifierFieldNames(), $this->entityIdentifiers[$oid]),
$entity,
$lockMode,
);

$this->cascadeRefresh($entity, $visited);
$this->cascadeRefresh($entity, $visited, $lockMode);
}

/**
* Cascades a refresh operation to associated entities.
*
* @psalm-param array<int, object> $visited
* @psalm-param LockMode::*|null $lockMode
*/
private function cascadeRefresh(object $entity, array &$visited): void
private function cascadeRefresh(object $entity, array &$visited, LockMode|int|null $lockMode = null): void
{
$class = $this->em->getClassMetadata($entity::class);

Expand All @@ -1854,13 +1875,13 @@ private function cascadeRefresh(object $entity, array &$visited): void
case $relatedEntities instanceof Collection:
case is_array($relatedEntities):
foreach ($relatedEntities as $relatedEntity) {
$this->doRefresh($relatedEntity, $visited);
$this->doRefresh($relatedEntity, $visited, $lockMode);
}

break;

case $relatedEntities !== null:
$this->doRefresh($relatedEntities, $visited);
$this->doRefresh($relatedEntities, $visited, $lockMode);
break;

default:
Expand Down
3 changes: 3 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@
<!-- Persistence 2 compatibility -->
<referencedFunction name="Doctrine\Persistence\ObjectManager::clear"/>

<!-- Remove on 3.0.x -->
<referencedFunction name="Doctrine\Persistence\ObjectManager::refresh"/>

<!-- See https://github.com/doctrine/orm/issues/8850 -->
<referencedFunction name="Doctrine\DBAL\Connection::lastInsertId"/>

Expand Down
48 changes: 48 additions & 0 deletions tests/Doctrine/Tests/ORM/Functional/Locking/LockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,21 @@ public function testLockPessimisticWriteNoTransactionThrowsException(): void
$this->_em->lock($article, LockMode::PESSIMISTIC_WRITE);
}

/** @group locking */
public function testRefreshWithLockPessimisticWriteNoTransactionThrowsException(): void
{
$article = new CmsArticle();
$article->text = 'my article';
$article->topic = 'Hello';

$this->_em->persist($article);
$this->_em->flush();

$this->expectException(TransactionRequiredException::class);

$this->_em->refresh($article, LockMode::PESSIMISTIC_WRITE);
}

/**
* @group DDC-178
* @group locking
Expand Down Expand Up @@ -180,6 +195,39 @@ public function testLockPessimisticWrite(): void
self::assertStringContainsString($writeLockSql, $lastLoggedQuery);
}

/** @group locking */
public function testRefreshWithLockPessimisticWrite(): void
{
$writeLockSql = $this->_em->getConnection()->getDatabasePlatform()->getWriteLockSQL();

if (! $writeLockSql) {
self::markTestSkipped('Database Driver has no Write Lock support.');
}

$article = new CmsArticle();
$article->text = 'my article';
$article->topic = 'Hello';

$this->_em->persist($article);
$this->_em->flush();

$this->_em->beginTransaction();
try {
$this->_em->refresh($article, LockMode::PESSIMISTIC_WRITE);
$this->_em->commit();
} catch (Exception) {
$this->_em->rollback();
}

$lastLoggedQuery = $this->getLastLoggedQuery()['sql'];
// DBAL 2 logs a commit as last query.
if ($lastLoggedQuery === '"COMMIT"') {
$lastLoggedQuery = $this->getLastLoggedQuery(1)['sql'];
}

self::assertStringContainsString($writeLockSql, $lastLoggedQuery);
}

/** @group DDC-178 */
public function testLockPessimisticRead(): void
{
Expand Down