1
0
mirror of synced 2025-02-20 22:23:14 +03:00

#1169 DDC-3343 - adapting cached collection persister logic to EXTRA_LAZY collection behavior

This commit is contained in:
Marco Pivetta 2015-01-25 04:40:30 +01:00
parent 01a9dadee7
commit 7e85c94f48
3 changed files with 40 additions and 26 deletions

View File

@ -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)
));
}
}

View File

@ -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");

View File

@ -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();