Merge branch 'hotfix/#1380-non-cache-persister-bug-2.5' into 2.5
Close #1380
This commit is contained in:
commit
12d178777a
@ -180,13 +180,7 @@ class DefaultCacheFactory implements CacheFactory
|
||||
*/
|
||||
public function buildCollectionHydrator(EntityManagerInterface $em, array $mapping)
|
||||
{
|
||||
/* @var $targetPersister \Doctrine\ORM\Cache\Persister\CachedPersister */
|
||||
$targetPersister = $em->getUnitOfWork()->getEntityPersister($mapping['targetEntity']);
|
||||
|
||||
return new DefaultCollectionHydrator(
|
||||
$em,
|
||||
$targetPersister->getCacheRegion()
|
||||
);
|
||||
return new DefaultCollectionHydrator($em);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -26,6 +26,11 @@ use Doctrine\Common\Persistence\Event\LoadClassMetadataEventArgs as BaseLoadClas
|
||||
*
|
||||
* @author Jonathan H. Wage <jonwage@gmail.com>
|
||||
* @since 2.0
|
||||
*
|
||||
* Note: method annotations are used instead of method overrides (due to BC policy)
|
||||
*
|
||||
* @method __construct(\Doctrine\ORM\Mapping\ClassMetadata $classMetadata, \Doctrine\ORM\EntityManager $objectManager)
|
||||
* @method \Doctrine\ORM\EntityManager getClassMetadata()
|
||||
*/
|
||||
class LoadClassMetadataEventArgs extends BaseLoadClassMetadataEventArgs
|
||||
{
|
||||
|
@ -3,33 +3,87 @@
|
||||
namespace Doctrine\Tests\EventListener;
|
||||
|
||||
use Doctrine\Common\Persistence\Event\LoadClassMetadataEventArgs;
|
||||
use Doctrine\ORM\EntityManager;
|
||||
use Doctrine\ORM\Mapping\ClassMetadata;
|
||||
|
||||
class CacheMetadataListener
|
||||
{
|
||||
|
||||
/**
|
||||
* Tracks which entities we have already forced caching enabled on. This is
|
||||
* important to avoid some potential infinite-recursion issues.
|
||||
*
|
||||
* Key is the name of the entity, payload is unimportant.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $enabledItems = array();
|
||||
|
||||
/**
|
||||
* @param \Doctrine\Common\Persistence\Event\LoadClassMetadataEventArgs $event
|
||||
*/
|
||||
public function loadClassMetadata(LoadClassMetadataEventArgs $event)
|
||||
{
|
||||
$metadata = $event->getClassMetadata();
|
||||
$cache = array(
|
||||
'usage' => ClassMetadata::CACHE_USAGE_NONSTRICT_READ_WRITE
|
||||
);
|
||||
$em = $event->getObjectManager();
|
||||
|
||||
/** @var $metadata \Doctrine\ORM\Mapping\ClassMetadata */
|
||||
if (strstr($metadata->name, 'Doctrine\Tests\Models\Cache')) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->enableCaching($metadata, $em);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ClassMetadata $metadata
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function isVisited(ClassMetaData $metadata)
|
||||
{
|
||||
return isset($this->enabledItems[$metadata->getName()]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ClassMetadata $metadata
|
||||
*/
|
||||
private function recordVisit(ClassMetaData $metadata)
|
||||
{
|
||||
$this->enabledItems[$metadata->getName()] = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ClassMetadata $metadata
|
||||
* @param EntityManager $em
|
||||
*/
|
||||
protected function enableCaching(ClassMetadata $metadata, EntityManager $em)
|
||||
{
|
||||
if ($this->isVisited($metadata)) {
|
||||
return; // Already handled in the past
|
||||
}
|
||||
|
||||
$cache = array(
|
||||
'usage' => ClassMetadata::CACHE_USAGE_NONSTRICT_READ_WRITE
|
||||
);
|
||||
|
||||
if ($metadata->isVersioned) {
|
||||
return;
|
||||
}
|
||||
|
||||
$metadata->enableCache($cache);
|
||||
|
||||
$this->recordVisit($metadata);
|
||||
|
||||
// only enable association-caching when the target has already been
|
||||
// given caching settings
|
||||
foreach ($metadata->associationMappings as $mapping) {
|
||||
$targetMeta = $em->getClassMetadata($mapping['targetEntity']);
|
||||
$this->enableCaching($targetMeta, $em);
|
||||
|
||||
if ($this->isVisited($targetMeta)) {
|
||||
$metadata->enableAssociationCache($mapping['fieldName'], $cache);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user