/** * {@inheritdoc} */ public function update(PersistentCollection $collection) { if ($collection->isDirty() && $collection->getSnapshot()) { throw CacheException::updateReadOnlyCollection(ClassUtils::getClass($collection->getOwner()), $this->association['fieldName']); } parent::update($collection); }
/** * {@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 storeEntityCache($entity, EntityCacheKey $key) { $class = $this->class; $className = ClassUtils::getClass($entity); if ($className !== $this->class->name) { $class = $this->metadataFactory->getMetadataFor($className); } if ($class->containsForeignIdentifier) { foreach ($class->associationMappings as $name => $assoc) { if (!empty($assoc['id']) && !isset($assoc['cache'])) { throw CacheException::nonCacheableEntityAssociation($class->name, $name); } } } $entry = $this->hydrator->buildCacheEntry($class, $key, $entity); $cached = $this->region->put($key, $entry); if ($this->cacheLogger && $cached) { $this->cacheLogger->entityCachePut($this->regionName, $key); } return $cached; }
/** * {@inheritdoc} */ public function update($entity) { throw CacheException::updateReadOnlyEntity(ClassUtils::getClass($entity)); }
/** * Validates & completes the basic mapping information that is common to all * association mappings (one-to-one, many-ot-one, one-to-many, many-to-many). * * @param array $mapping The mapping. * * @return array The updated mapping. * * @throws MappingException If something is wrong with the mapping. */ protected function _validateAndCompleteAssociationMapping(array $mapping) { if (!isset($mapping['mappedBy'])) { $mapping['mappedBy'] = null; } if (!isset($mapping['inversedBy'])) { $mapping['inversedBy'] = null; } $mapping['isOwningSide'] = true; // assume owning side until we hit mappedBy // unset optional indexBy attribute if its empty if (!isset($mapping['indexBy']) || !$mapping['indexBy']) { unset($mapping['indexBy']); } // If targetEntity is unqualified, assume it is in the same namespace as // the sourceEntity. $mapping['sourceEntity'] = $this->name; if (isset($mapping['targetEntity'])) { $mapping['targetEntity'] = $this->fullyQualifiedClassName($mapping['targetEntity']); $mapping['targetEntity'] = ltrim($mapping['targetEntity'], '\\'); } if (($mapping['type'] & self::MANY_TO_ONE) > 0 && isset($mapping['orphanRemoval']) && $mapping['orphanRemoval'] == true) { throw MappingException::illegalOrphanRemoval($this->name, $mapping['fieldName']); } // Complete id mapping if (isset($mapping['id']) && $mapping['id'] === true) { if (isset($mapping['orphanRemoval']) && $mapping['orphanRemoval'] == true) { throw MappingException::illegalOrphanRemovalOnIdentifierAssociation($this->name, $mapping['fieldName']); } if (!in_array($mapping['fieldName'], $this->identifier)) { if (isset($mapping['joinColumns']) && count($mapping['joinColumns']) >= 2) { throw MappingException::cannotMapCompositePrimaryKeyEntitiesAsForeignId($mapping['targetEntity'], $this->name, $mapping['fieldName']); } $this->identifier[] = $mapping['fieldName']; $this->containsForeignIdentifier = true; } // Check for composite key if (!$this->isIdentifierComposite && count($this->identifier) > 1) { $this->isIdentifierComposite = true; } if ($this->cache && !isset($mapping['cache'])) { throw CacheException::nonCacheableEntityAssociation($this->name, $mapping['fieldName']); } } // Mandatory attributes for both sides // Mandatory: fieldName, targetEntity if (!isset($mapping['fieldName']) || strlen($mapping['fieldName']) == 0) { throw MappingException::missingFieldName($this->name); } if (!isset($mapping['targetEntity'])) { throw MappingException::missingTargetEntity($mapping['fieldName']); } // Mandatory and optional attributes for either side if (!$mapping['mappedBy']) { if (isset($mapping['joinTable']) && $mapping['joinTable']) { if (isset($mapping['joinTable']['name']) && $mapping['joinTable']['name'][0] === '`') { $mapping['joinTable']['name'] = trim($mapping['joinTable']['name'], '`'); $mapping['joinTable']['quoted'] = true; } } } else { $mapping['isOwningSide'] = false; } if (isset($mapping['id']) && $mapping['id'] === true && $mapping['type'] & self::TO_MANY) { throw MappingException::illegalToManyIdentifierAssociation($this->name, $mapping['fieldName']); } // Fetch mode. Default fetch mode to LAZY, if not set. if (!isset($mapping['fetch'])) { $mapping['fetch'] = self::FETCH_LAZY; } // Cascades $cascades = isset($mapping['cascade']) ? array_map('strtolower', $mapping['cascade']) : array(); if (in_array('all', $cascades)) { $cascades = array('remove', 'persist', 'refresh', 'merge', 'detach'); } if (count($cascades) !== count(array_intersect($cascades, array('remove', 'persist', 'refresh', 'merge', 'detach')))) { throw MappingException::invalidCascadeOption(array_diff($cascades, array_intersect($cascades, array('remove', 'persist', 'refresh', 'merge', 'detach'))), $this->name, $mapping['fieldName']); } $mapping['cascade'] = $cascades; $mapping['isCascadeRemove'] = in_array('remove', $cascades); $mapping['isCascadePersist'] = in_array('persist', $cascades); $mapping['isCascadeRefresh'] = in_array('refresh', $cascades); $mapping['isCascadeMerge'] = in_array('merge', $cascades); $mapping['isCascadeDetach'] = in_array('detach', $cascades); return $mapping; }
/** * {@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)); }