/** * {@inheritdoc} */ public function put(QueryCacheKey $key, ResultSetMapping $rsm, $result, array $hints = array()) { if ($rsm->scalarMappings) { throw new CacheException("Second level cache does not support scalar results."); } if (count($rsm->entityMappings) > 1) { throw new CacheException("Second level cache does not support multiple root entities."); } if (!$rsm->isSelect) { throw new CacheException("Second-level cache query supports only select statements."); } if (isset($hints[Query::HINT_FORCE_PARTIAL_LOAD]) && $hints[Query::HINT_FORCE_PARTIAL_LOAD]) { throw new CacheException("Second level cache does not support partial entities."); } if (!($key->cacheMode & Cache::MODE_PUT)) { return false; } $data = array(); $entityName = reset($rsm->aliasMap); $hasRelation = !empty($rsm->relationMap); $metadata = $this->em->getClassMetadata($entityName); $persister = $this->uow->getEntityPersister($entityName); if (!$persister instanceof CachedPersister) { throw CacheException::nonCacheableEntity($entityName); } $region = $persister->getCacheRegion(); foreach ($result as $index => $entity) { $identifier = $this->uow->getEntityIdentifier($entity); $data[$index]['identifier'] = $identifier; $data[$index]['associations'] = array(); if ($key->cacheMode & Cache::MODE_REFRESH || !$region->contains($entityKey = new EntityCacheKey($entityName, $identifier))) { // Cancel put result if entity put fail if (!$persister->storeEntityCache($entity, $entityKey)) { return false; } } if (!$hasRelation) { continue; } // @TODO - move to cache hydration components foreach ($rsm->relationMap as $name) { $assoc = $metadata->associationMappings[$name]; if (($assocValue = $metadata->getFieldValue($entity, $name)) === null || $assocValue instanceof Proxy) { continue; } if (!isset($assoc['cache'])) { throw CacheException::nonCacheableEntityAssociation($entityName, $name); } $assocPersister = $this->uow->getEntityPersister($assoc['targetEntity']); $assocRegion = $assocPersister->getCacheRegion(); $assocMetadata = $assocPersister->getClassMetadata(); // Handle *-to-one associations if ($assoc['type'] & ClassMetadata::TO_ONE) { $assocIdentifier = $this->uow->getEntityIdentifier($assocValue); if ($key->cacheMode & Cache::MODE_REFRESH || !$assocRegion->contains($entityKey = new EntityCacheKey($assocMetadata->rootEntityName, $assocIdentifier))) { // Cancel put result if association entity put fail if (!$assocPersister->storeEntityCache($assocValue, $entityKey)) { return false; } } $data[$index]['associations'][$name] = array('targetEntity' => $assocMetadata->rootEntityName, 'identifier' => $assocIdentifier, 'type' => $assoc['type']); continue; } // Handle *-to-many associations $list = array(); foreach ($assocValue as $assocItemIndex => $assocItem) { $assocIdentifier = $this->uow->getEntityIdentifier($assocItem); if ($key->cacheMode & Cache::MODE_REFRESH || !$assocRegion->contains($entityKey = new EntityCacheKey($assocMetadata->rootEntityName, $assocIdentifier))) { // Cancel put result if entity put fail if (!$assocPersister->storeEntityCache($assocItem, $entityKey)) { return false; } } $list[$assocItemIndex] = $assocIdentifier; } $data[$index]['associations'][$name] = array('targetEntity' => $assocMetadata->rootEntityName, 'type' => $assoc['type'], 'list' => $list); } } return $this->region->put($key, new QueryCacheEntry($data)); }
/** * {@inheritdoc} */ public function put(QueryCacheKey $key, ResultSetMapping $rsm, $result, array $hints = []) { if ($rsm->scalarMappings) { throw new CacheException("Second level cache does not support scalar results."); } if (count($rsm->entityMappings) > 1) { throw new CacheException("Second level cache does not support multiple root entities."); } if (!$rsm->isSelect) { throw new CacheException("Second-level cache query supports only select statements."); } if (isset($hints[Query::HINT_FORCE_PARTIAL_LOAD]) && $hints[Query::HINT_FORCE_PARTIAL_LOAD]) { throw new CacheException("Second level cache does not support partial entities."); } if (!($key->cacheMode & Cache::MODE_PUT)) { return false; } $data = []; $entityName = reset($rsm->aliasMap); $rootAlias = key($rsm->aliasMap); $hasRelation = !empty($rsm->relationMap); $persister = $this->uow->getEntityPersister($entityName); if (!$persister instanceof CachedPersister) { throw CacheException::nonCacheableEntity($entityName); } $region = $persister->getCacheRegion(); foreach ($result as $index => $entity) { $identifier = $this->uow->getEntityIdentifier($entity); $entityKey = new EntityCacheKey($entityName, $identifier); $data[$index]['identifier'] = $identifier; $data[$index]['associations'] = []; if ($key->cacheMode & Cache::MODE_REFRESH || !$region->contains($entityKey)) { // Cancel put result if entity put fail if (!$persister->storeEntityCache($entity, $entityKey)) { return false; } } if (!$hasRelation) { continue; } // @TODO - move to cache hydration components foreach ($rsm->relationMap as $alias => $name) { $parentAlias = $rsm->parentAliasMap[$alias]; $parentClass = $rsm->aliasMap[$parentAlias]; $metadata = $this->em->getClassMetadata($parentClass); $assoc = $metadata->associationMappings[$name]; $assocValue = $this->getAssociationValue($rsm, $alias, $entity); if ($assocValue === null) { continue; } // root entity association if ($rootAlias === $parentAlias) { // Cancel put result if association put fail if (($assocInfo = $this->storeAssociationCache($key, $assoc, $assocValue)) === null) { return false; } $data[$index]['associations'][$name] = $assocInfo; continue; } // store single nested association if (!is_array($assocValue)) { // Cancel put result if association put fail if ($this->storeAssociationCache($key, $assoc, $assocValue) === null) { return false; } continue; } // store array of nested association foreach ($assocValue as $aVal) { // Cancel put result if association put fail if ($this->storeAssociationCache($key, $assoc, $aVal) === null) { return false; } } } } return $this->region->put($key, new QueryCacheEntry($data)); }