public function testLoadCacheCollection()
 {
     $targetRegion = $this->_em->getCache()->getEntityCacheRegion(City::CLASSNAME);
     $entry = new CollectionCacheEntry(array(new EntityCacheKey(City::CLASSNAME, array('id' => 31)), new EntityCacheKey(City::CLASSNAME, array('id' => 32))));
     $targetRegion->put(new EntityCacheKey(City::CLASSNAME, array('id' => 31)), new EntityCacheEntry(City::CLASSNAME, array('id' => 31, 'name' => 'Foo')));
     $targetRegion->put(new EntityCacheKey(City::CLASSNAME, array('id' => 32)), new EntityCacheEntry(City::CLASSNAME, array('id' => 32, 'name' => 'Bar')));
     $sourceClass = $this->_em->getClassMetadata(State::CLASSNAME);
     $targetClass = $this->_em->getClassMetadata(City::CLASSNAME);
     $key = new CollectionCacheKey($sourceClass->name, 'cities', array('id' => 21));
     $collection = new PersistentCollection($this->_em, $targetClass, new ArrayCollection());
     $list = $this->structure->loadCacheEntry($sourceClass, $key, $entry, $collection);
     $this->assertNotNull($list);
     $this->assertCount(2, $list);
     $this->assertCount(2, $collection);
     $this->assertInstanceOf($targetClass->name, $list[0]);
     $this->assertInstanceOf($targetClass->name, $list[1]);
     $this->assertInstanceOf($targetClass->name, $collection[0]);
     $this->assertInstanceOf($targetClass->name, $collection[1]);
     $this->assertSame($list[0], $collection[0]);
     $this->assertSame($list[1], $collection[1]);
     $this->assertEquals(31, $list[0]->getId());
     $this->assertEquals(32, $list[1]->getId());
     $this->assertEquals($list[0]->getId(), $collection[0]->getId());
     $this->assertEquals($list[1]->getId(), $collection[1]->getId());
     $this->assertEquals(UnitOfWork::STATE_MANAGED, $this->_em->getUnitOfWork()->getEntityState($collection[0]));
     $this->assertEquals(UnitOfWork::STATE_MANAGED, $this->_em->getUnitOfWork()->getEntityState($collection[1]));
 }
 /**
  * {@inheritdoc}
  */
 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;
         }
         $class = $this->targetEntity;
         $className = ClassUtils::getClass($elements[$index]);
         if ($className !== $this->targetEntity->name) {
             $class = $this->metadataFactory->getMetadataFor($className);
         }
         $entity = $elements[$index];
         $entityEntry = $targetHydrator->buildCacheEntry($class, $entityKey, $entity);
         $targetRegion->put($entityKey, $entityEntry);
     }
     $cached = $this->region->put($key, $entry);
     if ($this->cacheLogger && $cached) {
         $this->cacheLogger->collectionCachePut($this->regionName, $key);
     }
 }
 /**
  * {@inheritdoc}
  */
 public function storeCollectionCache(CollectionCacheKey $key, $elements)
 {
     /* @var $targetPersister CachedEntityPersister */
     $associationMapping = $this->sourceEntity->associationMappings[$key->association];
     $targetPersister = $this->uow->getEntityPersister($this->targetEntity->rootEntityName);
     $targetRegion = $targetPersister->getCacheRegion();
     $targetHydrator = $targetPersister->getEntityHydrator();
     // Only preserve ordering if association configured it
     if (!(isset($associationMapping['indexBy']) && $associationMapping['indexBy'])) {
         // Elements may be an array or a Collection
         $elements = array_values(is_array($elements) ? $elements : $elements->getValues());
     }
     $entry = $this->hydrator->buildCacheEntry($this->targetEntity, $key, $elements);
     foreach ($entry->identifiers as $index => $entityKey) {
         if ($targetRegion->contains($entityKey)) {
             continue;
         }
         $class = $this->targetEntity;
         $className = ClassUtils::getClass($elements[$index]);
         if ($className !== $this->targetEntity->name) {
             $class = $this->metadataFactory->getMetadataFor($className);
         }
         $entity = $elements[$index];
         $entityEntry = $targetHydrator->buildCacheEntry($class, $entityKey, $entity);
         $targetRegion->put($entityKey, $entityEntry);
     }
     $cached = $this->region->put($key, $entry);
     if ($this->cacheLogger && $cached) {
         $this->cacheLogger->collectionCachePut($this->regionName, $key);
     }
 }