Skip to content

Commit

Permalink
Add lockMode to EntityManager#refresh()
Browse files Browse the repository at this point in the history
  • Loading branch information
michnovka committed Sep 11, 2022
1 parent 498da2f commit e836f50
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 12 deletions.
7 changes: 4 additions & 3 deletions lib/Doctrine/ORM/EntityManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -693,22 +693,23 @@ public function remove($entity)
* Refreshes the persistent state of an entity from the database,
* overriding any local changes that have not yet been persisted.
*
* @param object $entity The entity to refresh.
* @param object $entity The entity to refresh
* @psalm-param LockMode::*|null $lockMode
*
* @return void
*
* @throws ORMInvalidArgumentException
* @throws ORMException
*/
public function refresh($entity)
public function refresh($entity, $lockMode = null)
{
if (! is_object($entity)) {
throw ORMInvalidArgumentException::invalidObject('EntityManager#refresh()', $entity);
}

$this->errorIfClosed();

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

/**
Expand Down
22 changes: 13 additions & 9 deletions lib/Doctrine/ORM/UnitOfWork.php
Original file line number Diff line number Diff line change
Expand Up @@ -2182,28 +2182,30 @@ private function doDetach(
* Refreshes the state of the given entity from the database, overwriting
* any local, unpersisted changes.
*
* @param object $entity The entity to refresh.
* @param object $entity The entity to refresh
* @psalm-param LockMode::*|null $lockMode
*
* @return void
*
* @throws InvalidArgumentException If the entity is not MANAGED.
*/
public function refresh($entity)
public function refresh($entity, $lockMode = null)
{
$visited = [];

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

/**
* Executes a refresh operation on an entity.
*
* @param object $entity The entity to refresh.
* @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.
*/
private function doRefresh($entity, array &$visited): void
private function doRefresh($entity, array &$visited, $lockMode = null): void
{
$oid = spl_object_id($entity);

Expand All @@ -2221,19 +2223,21 @@ private function doRefresh($entity, array &$visited): void

$this->getEntityPersister($class->name)->refresh(
array_combine($class->getIdentifierFieldNames(), $this->entityIdentifiers[$oid]),
$entity
$entity,
$lockMode
);

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

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

Expand All @@ -2256,13 +2260,13 @@ static function ($assoc) {
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
17 changes: 17 additions & 0 deletions tests/Doctrine/Tests/ORM/Functional/EntityRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,23 @@ public function testIdentityMappedOptimisticLockUnversionedEntityThrowsException
$this->_em->find(CmsUser::class, $userId, LockMode::OPTIMISTIC);
}

/**
* @group locking
*/
public function testRefreshWithLockThrowsException(): void
{
$user = new CmsUser();
$user->name = 'Roman';
$user->username = 'romanb';
$user->status = 'freak';
$this->_em->persist($user);
$this->_em->flush();

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

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

/** @group DDC-819 */
public function testFindMagicCallByNullValue(): void
{
Expand Down

0 comments on commit e836f50

Please sign in to comment.