Skip to content

Commit

Permalink
Merge pull request #8023 from peterkeatingie/query-cache-fix
Browse files Browse the repository at this point in the history
Put into cache using root entity name
  • Loading branch information
beberlei committed Feb 16, 2020
2 parents 445796a + 9bb2bf0 commit ca57222
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lib/Doctrine/ORM/Cache/DefaultQueryCache.php
Expand Up @@ -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
Expand Down
49 changes: 49 additions & 0 deletions tests/Doctrine/Tests/ORM/Functional/Ticket/DDC7969Test.php
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

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;

class DDC7969Test extends SecondLevelCacheAbstractTest
{
public function testChildEntityRetrievedFromCache() : void
{
$this->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());
}
}

0 comments on commit ca57222

Please sign in to comment.