From 1f53afa9cd28425132854e5ec35ae1c7024de408 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lu=C3=ADs=20Cobucci?= Date: Thu, 19 Jan 2017 17:19:34 +0100 Subject: [PATCH] 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). --- .../ORM/Cache/DefaultCollectionHydrator.php | 3 +- .../ORM/Cache/DefaultEntityHydrator.php | 3 +- lib/Doctrine/ORM/Cache/DefaultQueryCache.php | 9 +- .../Entity/AbstractEntityPersister.php | 3 +- .../ORM/Functional/Ticket/GH5562Test.php | 128 ++++++++++++++++++ 5 files changed, 138 insertions(+), 8 deletions(-) create mode 100644 tests/Doctrine/Tests/ORM/Functional/Ticket/GH5562Test.php diff --git a/lib/Doctrine/ORM/Cache/DefaultCollectionHydrator.php b/lib/Doctrine/ORM/Cache/DefaultCollectionHydrator.php index 7ff11e4adf1..f12ccf50a78 100644 --- a/lib/Doctrine/ORM/Cache/DefaultCollectionHydrator.php +++ b/lib/Doctrine/ORM/Cache/DefaultCollectionHydrator.php @@ -65,8 +65,9 @@ public function buildCacheEntry(ClassMetadata $metadata, CollectionCacheKey $key $data = []; 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); } diff --git a/lib/Doctrine/ORM/Cache/DefaultEntityHydrator.php b/lib/Doctrine/ORM/Cache/DefaultEntityHydrator.php index 99a98bddc77..7d72464dc01 100644 --- a/lib/Doctrine/ORM/Cache/DefaultEntityHydrator.php +++ b/lib/Doctrine/ORM/Cache/DefaultEntityHydrator.php @@ -175,7 +175,8 @@ public function loadCacheEntry(ClassMetadata $metadata, EntityCacheKey $key, Ent 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']); $assocRegion = $assocPersister->getCacheRegion(); $assocEntry = $assocRegion->get($assocKey); diff --git a/lib/Doctrine/ORM/Cache/DefaultQueryCache.php b/lib/Doctrine/ORM/Cache/DefaultQueryCache.php index b7ac6f00cb4..076fe3bebed 100644 --- a/lib/Doctrine/ORM/Cache/DefaultQueryCache.php +++ b/lib/Doctrine/ORM/Cache/DefaultQueryCache.php @@ -148,13 +148,13 @@ public function get(QueryCacheKey $key, ResultSetMapping $rsm, array $hints = [] $data = $entityEntry->data; foreach ($entry['associations'] as $name => $assoc) { - $assocPersister = $this->uow->getEntityPersister($assoc['targetEntity']); $assocRegion = $assocPersister->getCacheRegion(); + $assocMetadata = $this->em->getClassMetadata($assoc['targetEntity']); 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) { $this->cacheLogger->entityCacheMiss($assocRegion->getName(), $assocKey); @@ -178,12 +178,11 @@ public function get(QueryCacheKey $key, ResultSetMapping $rsm, array $hints = [] continue; } - $targetClass = $this->em->getClassMetadata($assoc['targetEntity']); - $collection = new PersistentCollection($this->em, $targetClass, new ArrayCollection()); + $collection = new PersistentCollection($this->em, $assocMetadata, new ArrayCollection()); 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) { $this->cacheLogger->entityCacheMiss($assocRegion->getName(), $assocKey); diff --git a/lib/Doctrine/ORM/Cache/Persister/Entity/AbstractEntityPersister.php b/lib/Doctrine/ORM/Cache/Persister/Entity/AbstractEntityPersister.php index a6e2b348036..1e0f17972cc 100644 --- a/lib/Doctrine/ORM/Cache/Persister/Entity/AbstractEntityPersister.php +++ b/lib/Doctrine/ORM/Cache/Persister/Entity/AbstractEntityPersister.php @@ -272,7 +272,8 @@ private function storeJoinedAssociations($entity) } $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->storeEntityCache($assocEntity, $assocKey); diff --git a/tests/Doctrine/Tests/ORM/Functional/Ticket/GH5562Test.php b/tests/Doctrine/Tests/ORM/Functional/Ticket/GH5562Test.php new file mode 100644 index 00000000000..824c4378add --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Functional/Ticket/GH5562Test.php @@ -0,0 +1,128 @@ +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; +}