From e8f91434a762ba5463318f36b1b19e39878c9efc Mon Sep 17 00:00:00 2001 From: "A.Kuterev" Date: Thu, 20 Jun 2019 17:28:04 +0300 Subject: [PATCH] Avoid reusing variable name The same variable name is used below, and that causes a bug etc. Fixes https://github.com/doctrine/orm/issues/7735 --- .../Entity/AbstractEntityPersister.php | 6 +- .../ORM/Functional/Ticket/GH7735Test.php | 172 ++++++++++++++++++ 2 files changed, 176 insertions(+), 2 deletions(-) create mode 100644 tests/Doctrine/Tests/ORM/Functional/Ticket/GH7735Test.php diff --git a/lib/Doctrine/ORM/Cache/Persister/Entity/AbstractEntityPersister.php b/lib/Doctrine/ORM/Cache/Persister/Entity/AbstractEntityPersister.php index 53ea6a6fde..05484120ec 100644 --- a/lib/Doctrine/ORM/Cache/Persister/Entity/AbstractEntityPersister.php +++ b/lib/Doctrine/ORM/Cache/Persister/Entity/AbstractEntityPersister.php @@ -452,12 +452,14 @@ public function loadById(array $identifier, $entity = null) $class = $this->metadataFactory->getMetadataFor($cacheEntry->class); } - if (($entity = $this->hydrator->loadCacheEntry($class, $cacheKey, $cacheEntry, $entity)) !== null) { + $cachedEntity = $this->hydrator->loadCacheEntry($class, $cacheKey, $cacheEntry, $entity); + + if ($cachedEntity !== null) { if ($this->cacheLogger) { $this->cacheLogger->entityCacheHit($this->regionName, $cacheKey); } - return $entity; + return $cachedEntity; } } diff --git a/tests/Doctrine/Tests/ORM/Functional/Ticket/GH7735Test.php b/tests/Doctrine/Tests/ORM/Functional/Ticket/GH7735Test.php new file mode 100644 index 0000000000..52a10dc643 --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Functional/Ticket/GH7735Test.php @@ -0,0 +1,172 @@ +enableSecondLevelCache(); + parent::setUp(); + + $this->_schemaTool->createSchema( + [ + $this->_em->getClassMetadata(GH7735Car::class), + $this->_em->getClassMetadata(GH7735Power::class), + $this->_em->getClassMetadata(GH7735Engine::class), + ] + ); + + $this->_em->persist(new GH7735Car(1, new GH7735Engine(1, 'turbo', new GH7735Power(1)))); + $this->_em->flush(); + $this->_em->clear(); + } + + /** + * @test + * @group GH7735 + */ + public function findByReturnsCachedEntity() : void + { + $this->_em->getCache()->evictEntityRegion(GH7735Power::class); + + $car = $this->_em->find(GH7735Car::class, 1); + assert($car instanceof GH7735Car); + + self::assertSame('turbo', $car->getEngine()->getModel()); + self::assertSame(1, $car->getEngine()->getPower()->getId()); + } +} + +/** + * @Entity @Cache(usage="READ_ONLY") + */ +class GH7735Car +{ + /** + * @Id + * @Column(type="integer") + * @var int + */ + private $id; + + /** + * @ManyToOne(targetEntity=GH7735Engine::class, cascade={"all"}) + * @JoinColumn(nullable=false) + * @Cache("READ_ONLY") + * @var GH7735Engine + */ + private $engine; + + public function __construct(int $id, GH7735Engine $engine) + { + $this->id = $id; + $this->engine = $engine; + } + + public function getId() : int + { + return $this->id; + } + + public function getEngine() : GH7735Engine + { + return $this->engine; + } +} + +/** + * @Entity + * @Cache(usage="READ_ONLY") + */ +class GH7735Engine +{ + /** + * @Id + * @Column(type="integer") + * @var int + */ + private $id; + + /** + * @OneToOne(targetEntity=GH7735Power::class, mappedBy="engine", cascade={"all"}) + * @Cache("READ_ONLY") + * @var GH7735Power + */ + private $power; + + /** + * @Column + * @var string + */ + private $model; + + public function __construct(int $id, string $model, GH7735Power $power) + { + $this->id = $id; + $this->model = $model; + $this->power = $power; + + $power->setEngine($this); + } + + public function getId() : int + { + return $this->id; + } + + public function getPower() : GH7735Power + { + return $this->power; + } + + public function getModel() : string + { + return $this->model; + } +} + +/** + * @Entity + * @Cache(usage="READ_ONLY") + */ +class GH7735Power +{ + /** + * @Id + * @Column(type="integer") + */ + private $id; + + /** + * @OneToOne(targetEntity=GH7735Engine::class, inversedBy="power") + * @Cache("READ_ONLY") + * @var GH7735Engine + */ + private $engine; + + public function __construct(int $id) + { + $this->id = $id; + } + + public function getId() : int + { + return $this->id; + } + + public function setEngine(GH7735Engine $engine) : void + { + $this->engine = $engine; + } + + public function getEngine() : GH7735Engine + { + return $this->engine; + } +}