From 9bb2bf0cce37df024c5cdedc1aa05bef6a6ec3f5 Mon Sep 17 00:00:00 2001 From: Peter Keating Date: Sat, 15 Feb 2020 15:53:47 +0000 Subject: [PATCH] Put into cache using root entity name --- lib/Doctrine/ORM/Cache/DefaultQueryCache.php | 7 ++- .../ORM/Functional/Ticket/DDC7969Test.php | 49 +++++++++++++++++++ 2 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 tests/Doctrine/Tests/ORM/Functional/Ticket/DDC7969Test.php diff --git a/lib/Doctrine/ORM/Cache/DefaultQueryCache.php b/lib/Doctrine/ORM/Cache/DefaultQueryCache.php index 866ea0db257..96f0fbd0ed6 100644 --- a/lib/Doctrine/ORM/Cache/DefaultQueryCache.php +++ b/lib/Doctrine/ORM/Cache/DefaultQueryCache.php @@ -279,9 +279,12 @@ public function put(QueryCacheKey $key, ResultSetMapping $rsm, $result, array $h $region = $persister->getCacheRegion(); + $cm = $this->em->getClassMetadata($entityName); + assert($cm instanceof ClassMetadata); + foreach ($result as $index => $entity) { - $identifier = $this->uow->getEntityIdentifier($entity); - $entityKey = new EntityCacheKey($entityName, $identifier); + $identifier = $this->uow->getEntityIdentifier($entity); + $entityKey = new EntityCacheKey($cm->rootEntityName, $identifier); if (($key->cacheMode & Cache::MODE_REFRESH) || ! $region->contains($entityKey)) { // Cancel put result if entity put fail diff --git a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC7969Test.php b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC7969Test.php new file mode 100644 index 00000000000..7d9592714e5 --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC7969Test.php @@ -0,0 +1,49 @@ +loadFixturesCountries(); + $this->loadFixturesStates(); + $this->loadFixturesCities(); + $this->loadFixturesAttractions(); + + // Entities are already cached due to fixtures - hence flush before testing + $region = $this->cache->getEntityCacheRegion(Attraction::class); + + if ($region instanceof DefaultMultiGetRegion) { + $region->getCache()->flushAll(); + } + + /** @var Bar $bar */ + $bar = $this->attractions[0]; + + $repository = $this->_em->getRepository(Bar::class); + + $this->assertFalse($this->cache->containsEntity(Bar::class, $bar->getId())); + $this->assertFalse($this->cache->containsEntity(Attraction::class, $bar->getId())); + + $repository->findOneBy([ + 'name' => $bar->getName(), + ]); + + $this->assertTrue($this->cache->containsEntity(Bar::class, $bar->getId())); + + $repository->findOneBy([ + 'name' => $bar->getName(), + ]); + + // One hit for entity cache, one hit for query cache + $this->assertEquals(2, $this->secondLevelCacheLogger->getHitCount()); + } +}