1
0
mirror of synced 2025-02-02 21:41:45 +03:00

Make sure we're using the rootEntityName on all places

Otherwise we might end up with duplicated cache entries and weird
results (specially regarding associations).
This commit is contained in:
Luís Cobucci 2017-01-19 17:19:34 +01:00
parent 4f28aaa206
commit 1f53afa9cd
No known key found for this signature in database
GPG Key ID: 8042585A7DBC92E1
5 changed files with 138 additions and 8 deletions

View File

@ -65,8 +65,9 @@ class DefaultCollectionHydrator implements CollectionHydrator
$data = []; $data = [];
foreach ($collection as $index => $entity) { foreach ($collection as $index => $entity) {
$data[$index] = new EntityCacheKey($metadata->name, $this->uow->getEntityIdentifier($entity)); $data[$index] = new EntityCacheKey($metadata->rootEntityName, $this->uow->getEntityIdentifier($entity));
} }
return new CollectionCacheEntry($data); return new CollectionCacheEntry($data);
} }

View File

@ -175,7 +175,8 @@ class DefaultEntityHydrator implements EntityHydrator
continue; continue;
} }
$assocKey = new EntityCacheKey($assoc['targetEntity'], $assocId); $assocMetadata = $this->em->getClassMetadata($assoc['targetEntity']);
$assocKey = new EntityCacheKey($assocMetadata->rootEntityName, $assocId);
$assocPersister = $this->uow->getEntityPersister($assoc['targetEntity']); $assocPersister = $this->uow->getEntityPersister($assoc['targetEntity']);
$assocRegion = $assocPersister->getCacheRegion(); $assocRegion = $assocPersister->getCacheRegion();
$assocEntry = $assocRegion->get($assocKey); $assocEntry = $assocRegion->get($assocKey);

View File

@ -148,13 +148,13 @@ class DefaultQueryCache implements QueryCache
$data = $entityEntry->data; $data = $entityEntry->data;
foreach ($entry['associations'] as $name => $assoc) { foreach ($entry['associations'] as $name => $assoc) {
$assocPersister = $this->uow->getEntityPersister($assoc['targetEntity']); $assocPersister = $this->uow->getEntityPersister($assoc['targetEntity']);
$assocRegion = $assocPersister->getCacheRegion(); $assocRegion = $assocPersister->getCacheRegion();
$assocMetadata = $this->em->getClassMetadata($assoc['targetEntity']);
if ($assoc['type'] & ClassMetadata::TO_ONE) { if ($assoc['type'] & ClassMetadata::TO_ONE) {
if (($assocEntry = $assocRegion->get($assocKey = new EntityCacheKey($assoc['targetEntity'], $assoc['identifier']))) === null) { if (($assocEntry = $assocRegion->get($assocKey = new EntityCacheKey($assocMetadata->rootEntityName, $assoc['identifier']))) === null) {
if ($this->cacheLogger !== null) { if ($this->cacheLogger !== null) {
$this->cacheLogger->entityCacheMiss($assocRegion->getName(), $assocKey); $this->cacheLogger->entityCacheMiss($assocRegion->getName(), $assocKey);
@ -178,12 +178,11 @@ class DefaultQueryCache implements QueryCache
continue; continue;
} }
$targetClass = $this->em->getClassMetadata($assoc['targetEntity']); $collection = new PersistentCollection($this->em, $assocMetadata, new ArrayCollection());
$collection = new PersistentCollection($this->em, $targetClass, new ArrayCollection());
foreach ($assoc['list'] as $assocIndex => $assocId) { foreach ($assoc['list'] as $assocIndex => $assocId) {
if (($assocEntry = $assocRegion->get($assocKey = new EntityCacheKey($assoc['targetEntity'], $assocId))) === null) { if (($assocEntry = $assocRegion->get($assocKey = new EntityCacheKey($assocMetadata->rootEntityName, $assocId))) === null) {
if ($this->cacheLogger !== null) { if ($this->cacheLogger !== null) {
$this->cacheLogger->entityCacheMiss($assocRegion->getName(), $assocKey); $this->cacheLogger->entityCacheMiss($assocRegion->getName(), $assocKey);

View File

@ -272,7 +272,8 @@ abstract class AbstractEntityPersister implements CachedEntityPersister
} }
$assocId = $this->uow->getEntityIdentifier($assocEntity); $assocId = $this->uow->getEntityIdentifier($assocEntity);
$assocKey = new EntityCacheKey($assoc['targetEntity'], $assocId); $assocMetadata = $this->metadataFactory->getMetadataFor($assoc['targetEntity']);
$assocKey = new EntityCacheKey($assocMetadata->rootEntityName, $assocId);
$assocPersister = $this->uow->getEntityPersister($assoc['targetEntity']); $assocPersister = $this->uow->getEntityPersister($assoc['targetEntity']);
$assocPersister->storeEntityCache($assocEntity, $assocKey); $assocPersister->storeEntityCache($assocEntity, $assocKey);

View File

@ -0,0 +1,128 @@
<?php
namespace Doctrine\Tests\ORM\Functional\Ticket;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\StringType;
use Doctrine\DBAL\Types\Type;
use Doctrine\ORM\AbstractQuery;
use Doctrine\Tests\OrmFunctionalTestCase;
final class GH5562Test extends OrmFunctionalTestCase
{
protected function setUp()
{
$this->enableSecondLevelCache();
parent::setUp();
$this->_schemaTool->createSchema(
[
$this->_em->getClassMetadata(GH5562User::class),
$this->_em->getClassMetadata(GH5562Manager::class),
$this->_em->getClassMetadata(GH5562Merchant::class),
]
);
}
/**
* @group 5562
*/
public function testCacheShouldBeUpdatedWhenAssociationChanges()
{
$manager = new GH5562Manager();
$merchant = new GH5562Merchant();
$manager->username = 'username';
$manager->merchant = $merchant;
$merchant->manager = $manager;
$merchant->name = 'Merchant';
$this->_em->persist($merchant);
$this->_em->persist($manager);
$this->_em->flush();
$this->_em->clear();
$merchant = $this->_em->find(GH5562Merchant::class, $merchant->id);
$merchant->name = mt_rand();
$merchant->manager->username = 'usernameUPDATE';
$this->_em->flush();
$this->_em->clear();
$merchant = $this->_em->find(GH5562Merchant::class, $merchant->id);
self::assertEquals('usernameUPDATE', $merchant->manager->username);
}
}
/**
* @Entity
* @Cache(usage="NONSTRICT_READ_WRITE")
*/
class GH5562Merchant
{
/**
* @var integer
*
* @Id
* @Column(name="id", type="integer")
* @GeneratedValue(strategy="IDENTITY")
*/
public $id;
/**
* @var GH5562Manager
*
* @OneToOne(targetEntity=GH5562Manager::class, mappedBy="merchant")
* @Cache(usage="NONSTRICT_READ_WRITE")
*/
public $manager;
/**
* @var string
*
* @Column(name="name", type="string", length=255, nullable=false)
*/
public $name;
}
/**
* @Entity
* @InheritanceType("SINGLE_TABLE")
* @DiscriminatorMap({"MANAGER" = GH5562Manager::class})
*/
abstract class GH5562User
{
/**
* @var integer
*
* @Id
* @Column(name="id", type="integer")
* @GeneratedValue(strategy="IDENTITY")
*/
public $id;
}
/**
* @Entity
* @Cache(usage="NONSTRICT_READ_WRITE")
*/
class GH5562Manager extends GH5562User
{
/**
* @var string
*
* @Column
*/
public $username;
/**
* @var GH5562Merchant
*
* @OneToOne(targetEntity=GH5562Merchant::class, inversedBy="manager")
*/
public $merchant;
}