From a999918a165cb35187599a1136d40bee47e8c21c Mon Sep 17 00:00:00 2001 From: Peter Keating Date: Fri, 31 Jan 2020 16:14:04 +0000 Subject: [PATCH 1/8] Put into cache using root entity name --- lib/Doctrine/ORM/Cache/DefaultQueryCache.php | 4 +- .../ORM/Functional/Ticket/DDC7969Test.php | 41 +++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) 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..82d5ee3dd88 100644 --- a/lib/Doctrine/ORM/Cache/DefaultQueryCache.php +++ b/lib/Doctrine/ORM/Cache/DefaultQueryCache.php @@ -279,9 +279,11 @@ public function put(QueryCacheKey $key, ResultSetMapping $rsm, $result, array $h $region = $persister->getCacheRegion(); + $cm = $this->em->getClassMetadata($entityName); + foreach ($result as $index => $entity) { $identifier = $this->uow->getEntityIdentifier($entity); - $entityKey = new EntityCacheKey($entityName, $identifier); + $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..01586b65fbc --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC7969Test.php @@ -0,0 +1,41 @@ +loadFixturesCountries(); + $this->loadFixturesStates(); + $this->loadFixturesCities(); + $this->loadFixturesAttractions(); + + // Entities are already cached due to fixtures - hence flush before testing + $this->cache->getEntityCacheRegion(Attraction::class)->getCache()->flushAll(); + + /** @var Bar $bar */ + $bar = $this->attractions[0]; + + $repository = $this->_em->getRepository(Bar::class); + + $this->assertFalse($this->cache->containsEntity(Bar::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()); + } +} From 87292e85314ec6b6d374eae5fc4ff4465721880c Mon Sep 17 00:00:00 2001 From: Peter Keating Date: Fri, 31 Jan 2020 16:55:20 +0000 Subject: [PATCH 2/8] Coding standards fixes --- lib/Doctrine/ORM/Cache/DefaultQueryCache.php | 2 +- tests/Doctrine/Tests/ORM/Functional/Ticket/DDC7969Test.php | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/Doctrine/ORM/Cache/DefaultQueryCache.php b/lib/Doctrine/ORM/Cache/DefaultQueryCache.php index 82d5ee3dd88..c3745be1c3e 100644 --- a/lib/Doctrine/ORM/Cache/DefaultQueryCache.php +++ b/lib/Doctrine/ORM/Cache/DefaultQueryCache.php @@ -283,7 +283,7 @@ public function put(QueryCacheKey $key, ResultSetMapping $rsm, $result, array $h foreach ($result as $index => $entity) { $identifier = $this->uow->getEntityIdentifier($entity); - $entityKey = new EntityCacheKey($cm->rootEntityName, $identifier); + $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 index 01586b65fbc..a099de3a584 100644 --- a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC7969Test.php +++ b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC7969Test.php @@ -1,5 +1,7 @@ Date: Fri, 31 Jan 2020 19:26:31 +0000 Subject: [PATCH 3/8] CS fix --- lib/Doctrine/ORM/Cache/DefaultQueryCache.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Doctrine/ORM/Cache/DefaultQueryCache.php b/lib/Doctrine/ORM/Cache/DefaultQueryCache.php index c3745be1c3e..147312a6476 100644 --- a/lib/Doctrine/ORM/Cache/DefaultQueryCache.php +++ b/lib/Doctrine/ORM/Cache/DefaultQueryCache.php @@ -283,7 +283,7 @@ public function put(QueryCacheKey $key, ResultSetMapping $rsm, $result, array $h foreach ($result as $index => $entity) { $identifier = $this->uow->getEntityIdentifier($entity); - $entityKey = new EntityCacheKey($cm->rootEntityName, $identifier); + $entityKey = new EntityCacheKey($cm->rootEntityName, $identifier); if (($key->cacheMode & Cache::MODE_REFRESH) || ! $region->contains($entityKey)) { // Cancel put result if entity put fail From 8120eb36727213ad28167bc62c47216ee54867a2 Mon Sep 17 00:00:00 2001 From: Peter Keating Date: Sat, 1 Feb 2020 17:26:48 +0000 Subject: [PATCH 4/8] Fix scrutinizer issues --- lib/Doctrine/ORM/Cache/DefaultQueryCache.php | 2 +- tests/Doctrine/Tests/ORM/Functional/Ticket/DDC7969Test.php | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/Doctrine/ORM/Cache/DefaultQueryCache.php b/lib/Doctrine/ORM/Cache/DefaultQueryCache.php index 147312a6476..6085285cde6 100644 --- a/lib/Doctrine/ORM/Cache/DefaultQueryCache.php +++ b/lib/Doctrine/ORM/Cache/DefaultQueryCache.php @@ -283,7 +283,7 @@ public function put(QueryCacheKey $key, ResultSetMapping $rsm, $result, array $h foreach ($result as $index => $entity) { $identifier = $this->uow->getEntityIdentifier($entity); - $entityKey = new EntityCacheKey($cm->rootEntityName, $identifier); + $entityKey = new EntityCacheKey($cm->getMetadataValue('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 index a099de3a584..ebac803172e 100644 --- a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC7969Test.php +++ b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC7969Test.php @@ -4,6 +4,7 @@ namespace tests\Doctrine\Tests\ORM\Functional\Ticket; +use Doctrine\ORM\Cache\Region\DefaultMultiGetRegion; use Doctrine\Tests\Models\Cache\Attraction; use Doctrine\Tests\Models\Cache\Bar; use Doctrine\Tests\ORM\Functional\SecondLevelCacheAbstractTest; @@ -18,7 +19,11 @@ public function testChildEntityRetrievedFromCache() $this->loadFixturesAttractions(); // Entities are already cached due to fixtures - hence flush before testing - $this->cache->getEntityCacheRegion(Attraction::class)->getCache()->flushAll(); + $region = $this->cache->getEntityCacheRegion(Attraction::class); + + if ($region instanceof DefaultMultiGetRegion) { + $region->getCache()->flushAll(); + } /** @var Bar $bar */ $bar = $this->attractions[0]; From c813064b45ff086d44bfa48d6a89e9d0c0e3c234 Mon Sep 17 00:00:00 2001 From: Peter Keating Date: Tue, 4 Feb 2020 08:34:12 +0000 Subject: [PATCH 5/8] Move method call, fix alignment --- lib/Doctrine/ORM/Cache/DefaultQueryCache.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/Doctrine/ORM/Cache/DefaultQueryCache.php b/lib/Doctrine/ORM/Cache/DefaultQueryCache.php index 6085285cde6..4433f27fb04 100644 --- a/lib/Doctrine/ORM/Cache/DefaultQueryCache.php +++ b/lib/Doctrine/ORM/Cache/DefaultQueryCache.php @@ -279,11 +279,12 @@ public function put(QueryCacheKey $key, ResultSetMapping $rsm, $result, array $h $region = $persister->getCacheRegion(); - $cm = $this->em->getClassMetadata($entityName); + $cm = $this->em->getClassMetadata($entityName); + $rootEntityName = $cm->getMetadataValue('rootEntityName'); foreach ($result as $index => $entity) { - $identifier = $this->uow->getEntityIdentifier($entity); - $entityKey = new EntityCacheKey($cm->getMetadataValue('rootEntityName'), $identifier); + $identifier = $this->uow->getEntityIdentifier($entity); + $entityKey = new EntityCacheKey($rootEntityName, $identifier); if (($key->cacheMode & Cache::MODE_REFRESH) || ! $region->contains($entityKey)) { // Cancel put result if entity put fail From f8c1f0ae1a0c0e34513aea1c3731fa6cf9b5e422 Mon Sep 17 00:00:00 2001 From: Peter Keating Date: Tue, 4 Feb 2020 08:34:22 +0000 Subject: [PATCH 6/8] Add return type --- tests/Doctrine/Tests/ORM/Functional/Ticket/DDC7969Test.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC7969Test.php b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC7969Test.php index ebac803172e..eea0ccfeac6 100644 --- a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC7969Test.php +++ b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC7969Test.php @@ -11,7 +11,7 @@ class DDC7969Test extends SecondLevelCacheAbstractTest { - public function testChildEntityRetrievedFromCache() + public function testChildEntityRetrievedFromCache(): void { $this->loadFixturesCountries(); $this->loadFixturesStates(); From ba0785c4347127bcdf0f1c35dadd552584e4b4be Mon Sep 17 00:00:00 2001 From: Peter Keating Date: Tue, 4 Feb 2020 11:47:49 +0000 Subject: [PATCH 7/8] Add space before colon --- tests/Doctrine/Tests/ORM/Functional/Ticket/DDC7969Test.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC7969Test.php b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC7969Test.php index eea0ccfeac6..717c62f6217 100644 --- a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC7969Test.php +++ b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC7969Test.php @@ -11,7 +11,7 @@ class DDC7969Test extends SecondLevelCacheAbstractTest { - public function testChildEntityRetrievedFromCache(): void + public function testChildEntityRetrievedFromCache() : void { $this->loadFixturesCountries(); $this->loadFixturesStates(); From e520a98fa6c4d3a505eacf92ab97868fffeecf94 Mon Sep 17 00:00:00 2001 From: Peter Keating Date: Thu, 13 Feb 2020 21:05:59 +0000 Subject: [PATCH 8/8] Use property directly. Update test. --- lib/Doctrine/ORM/Cache/DefaultQueryCache.php | 6 +++--- tests/Doctrine/Tests/ORM/Functional/Ticket/DDC7969Test.php | 1 + 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/Doctrine/ORM/Cache/DefaultQueryCache.php b/lib/Doctrine/ORM/Cache/DefaultQueryCache.php index 4433f27fb04..96f0fbd0ed6 100644 --- a/lib/Doctrine/ORM/Cache/DefaultQueryCache.php +++ b/lib/Doctrine/ORM/Cache/DefaultQueryCache.php @@ -279,12 +279,12 @@ public function put(QueryCacheKey $key, ResultSetMapping $rsm, $result, array $h $region = $persister->getCacheRegion(); - $cm = $this->em->getClassMetadata($entityName); - $rootEntityName = $cm->getMetadataValue('rootEntityName'); + $cm = $this->em->getClassMetadata($entityName); + assert($cm instanceof ClassMetadata); foreach ($result as $index => $entity) { $identifier = $this->uow->getEntityIdentifier($entity); - $entityKey = new EntityCacheKey($rootEntityName, $identifier); + $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 index 717c62f6217..7d9592714e5 100644 --- a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC7969Test.php +++ b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC7969Test.php @@ -31,6 +31,7 @@ public function testChildEntityRetrievedFromCache() : void $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(),