Skip to content

Commit

Permalink
doctrine#7068 early exit if arguments are not sane for given lockMode
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Kühn committed Feb 17, 2018
1 parent 0336d5a commit 6b2225a
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions lib/Doctrine/ORM/EntityManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,18 @@ public function find($entityName, $id, $lockMode = null, $lockVersion = null)
$class = $this->metadataFactory->getMetadataFor(ltrim($entityName, '\\'));
$className = $class->getClassName();

switch ($lockMode) {
case LockMode::OPTIMISTIC:
if ( ! $class->isVersioned()) {
throw OptimisticLockException::notVersioned($className);
}
case LockMode::PESSIMISTIC_READ:
case LockMode::PESSIMISTIC_WRITE:
if (! $this->getConnection()->isTransactionActive()) {
throw TransactionRequiredException::transactionRequired();
}
}

if (! is_array($id)) {
if ($class->isIdentifierComposite()) {
throw ORMInvalidArgumentException::invalidCompositeIdentifier();
Expand Down Expand Up @@ -446,25 +458,15 @@ public function find($entityName, $id, $lockMode = null, $lockVersion = null)
$persister = $unitOfWork->getEntityPersister($className);

switch (true) {
case $lockMode === LockMode::OPTIMISTIC:
if (! $class->isVersioned()) {
throw OptimisticLockException::notVersioned($className);
}

case$lockMode === LockMode::OPTIMISTIC:
$entity = $persister->load($sortedId);

$unitOfWork->lock($entity, $lockMode, $lockVersion);

return $entity;

case $lockMode === LockMode::PESSIMISTIC_READ:
case $lockMode === LockMode::PESSIMISTIC_WRITE:
if (! $this->getConnection()->isTransactionActive()) {
throw TransactionRequiredException::transactionRequired();
}

return $persister->load($sortedId, null, null, [], $lockMode);

default:
return $persister->loadById($sortedId);
}
Expand Down

0 comments on commit 6b2225a

Please sign in to comment.