Fix for BC break #7366 when calling EM::find() with LockMode::OPTIMISTIC outside of a TX

This commit is contained in:
Tim Lieberman
2018-08-22 11:49:47 -07:00
committed by Michael Moravec
parent 2f520244df
commit ce76688865
2 changed files with 79 additions and 1 deletions

View File

@@ -904,7 +904,7 @@ final class EntityManager implements EntityManagerInterface
if (! $class->isVersioned()) {
throw OptimisticLockException::notVersioned($class->getClassName());
}
// Intentional fallthrough
break;
case LockMode::PESSIMISTIC_READ:
case LockMode::PESSIMISTIC_WRITE:
if (! $this->getConnection()->isTransactionActive()) {

View File

@@ -0,0 +1,78 @@
<?php
declare(strict_types=1);
namespace Doctrine\Tests\ORM\Functional\Ticket;
use Doctrine\DBAL\LockMode;
use Doctrine\ORM\Annotation as ORM;
use Doctrine\ORM\TransactionRequiredException;
use Doctrine\Tests\OrmFunctionalTestCase;
final class GH7366Test extends OrmFunctionalTestCase
{
/**
* {@inheritDoc}
*/
protected function setUp() : void
{
parent::setUp();
$this->setUpEntitySchema(
[
GH7366Entity::class,
]
);
$this->em->persist(new GH7366Entity('baz'));
$this->em->flush();
$this->em->clear();
}
public function testOptimisticLockNoExceptionOnFind() : void
{
try {
$entity = $this->em->find(GH7366Entity::class, 1, LockMode::OPTIMISTIC);
} catch (TransactionRequiredException $e) {
self::fail('EntityManager::find() threw TransactionRequiredException with LockMode::OPTIMISTIC');
}
self::assertEquals('baz', $entity->getName());
}
}
/**
* @ORM\Entity
*/
class GH7366Entity
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue
* @var int
*/
public $id;
/**
* @ORM\Column(type="integer")
* @ORM\Version
*/
protected $lockVersion = 1;
/**
* @ORM\Column(length=32)
* @var string
*/
protected $name;
public function __construct(string $name)
{
$this->name = $name;
}
public function getName() : string
{
return $this->name;
}
}