From 7e85c94f4812e5f40fa1068b62fb2bad49d69b3b Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Sun, 25 Jan 2015 04:40:30 +0100 Subject: [PATCH] #1169 DDC-3343 - adapting cached collection persister logic to EXTRA_LAZY collection behavior --- .../AbstractCollectionPersister.php | 46 +++++++++++++++---- .../AbstractCollectionPersisterTest.php | 16 ------- .../Functional/ExtraLazyCollectionTest.php | 4 +- 3 files changed, 40 insertions(+), 26 deletions(-) diff --git a/lib/Doctrine/ORM/Cache/Persister/Collection/AbstractCollectionPersister.php b/lib/Doctrine/ORM/Cache/Persister/Collection/AbstractCollectionPersister.php index f62156763..7c21535cd 100644 --- a/lib/Doctrine/ORM/Cache/Persister/Collection/AbstractCollectionPersister.php +++ b/lib/Doctrine/ORM/Cache/Persister/Collection/AbstractCollectionPersister.php @@ -23,6 +23,7 @@ namespace Doctrine\ORM\Cache\Persister\Collection; use Doctrine\Common\Collections\Criteria; use Doctrine\ORM\Cache\EntityCacheKey; use Doctrine\ORM\Cache\CollectionCacheKey; +use Doctrine\ORM\Cache\Persister\Entity\CachedEntityPersister; use Doctrine\ORM\Persisters\Collection\CollectionPersister; use Doctrine\ORM\PersistentCollection; use Doctrine\ORM\EntityManagerInterface; @@ -162,13 +163,13 @@ abstract class AbstractCollectionPersister implements CachedCollectionPersister */ public function storeCollectionCache(CollectionCacheKey $key, $elements) { + /* @var $targetPersister CachedEntityPersister */ $targetPersister = $this->uow->getEntityPersister($this->targetEntity->rootEntityName); $targetRegion = $targetPersister->getCacheRegion(); $targetHydrator = $targetPersister->getEntityHydrator(); $entry = $this->hydrator->buildCacheEntry($this->targetEntity, $key, $elements); foreach ($entry->identifiers as $index => $entityKey) { - if ($targetRegion->contains($entityKey)) { continue; } @@ -238,15 +239,13 @@ abstract class AbstractCollectionPersister implements CachedCollectionPersister */ public function removeElement(PersistentCollection $collection, $element) { - return $this->persister->removeElement($collection, $element); - } + if ($persisterResult = $this->persister->removeElement($collection, $element)) { + $this->evictCollectionCache($collection); + $this->evictElementCache($this->sourceEntity->rootEntityName, $collection->getOwner()); + $this->evictElementCache($this->targetEntity->rootEntityName, $element); + } - /** - * {@inheritdoc} - */ - public function removeKey(PersistentCollection $collection, $key) - { - return $this->persister->removeKey($collection, $key); + return $persisterResult; } /** @@ -264,4 +263,33 @@ abstract class AbstractCollectionPersister implements CachedCollectionPersister { return $this->persister->loadCriteria($collection, $criteria); } + + /** + * Clears cache entries related to the current collection + * + * @param PersistentCollection $collection + */ + protected function evictCollectionCache(PersistentCollection $collection) + { + $this->region->evict(new CollectionCacheKey( + $this->sourceEntity->rootEntityName, + $this->association['fieldName'], + $this->uow->getEntityIdentifier($collection->getOwner()) + )); + } + + /** + * @param string $targetEntity + * @param object $element + */ + protected function evictElementCache($targetEntity, $element) + { + /* @var $targetPersister CachedEntityPersister */ + $targetPersister = $this->uow->getEntityPersister($targetEntity); + + $targetPersister->getCacheRegion()->evict(new EntityCacheKey( + $targetEntity, + $this->uow->getEntityIdentifier($element) + )); + } } diff --git a/tests/Doctrine/Tests/ORM/Cache/Persister/Collection/AbstractCollectionPersisterTest.php b/tests/Doctrine/Tests/ORM/Cache/Persister/Collection/AbstractCollectionPersisterTest.php index 17e7c4d7e..0d767aca9 100644 --- a/tests/Doctrine/Tests/ORM/Cache/Persister/Collection/AbstractCollectionPersisterTest.php +++ b/tests/Doctrine/Tests/ORM/Cache/Persister/Collection/AbstractCollectionPersisterTest.php @@ -240,22 +240,6 @@ abstract class AbstractCollectionPersisterTest extends OrmTestCase $this->assertFalse($persister->removeElement($collection, $element)); } - public function testInvokeRemoveKey() - { - $entity = new State("Foo"); - $persister = $this->createPersisterDefault(); - $collection = $this->createCollection($entity); - - $this->em->getUnitOfWork()->registerManaged($entity, array('id'=>1), array('id'=>1, 'name'=>'Foo')); - - $this->collectionPersister->expects($this->once()) - ->method('removeKey') - ->with($this->equalTo($collection), $this->equalTo(0)) - ->will($this->returnValue(false)); - - $this->assertFalse($persister->removeKey($collection, 0)); - } - public function testInvokeGet() { $entity = new State("Foo"); diff --git a/tests/Doctrine/Tests/ORM/Functional/ExtraLazyCollectionTest.php b/tests/Doctrine/Tests/ORM/Functional/ExtraLazyCollectionTest.php index 525021ff3..6021a9efd 100644 --- a/tests/Doctrine/Tests/ORM/Functional/ExtraLazyCollectionTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/ExtraLazyCollectionTest.php @@ -1062,7 +1062,9 @@ class ExtraLazyCollectionTest extends OrmFunctionalTestCase /* @var $user User */ $user = $this->_em->find(User::CLASSNAME, $userId); - $user->tweets->removeElement($this->_em->find(Tweet::CLASSNAME, $tweetId)); + $e = $this->_em->find(Tweet::CLASSNAME, $tweetId); + + $user->tweets->removeElement($e); $this->_em->clear();