/** * @param ClassMetadata $classMetadata * @return $this * * @throws MappingException */ private function fromDocument(ClassMetadata $classMetadata) { if ($classMetadata->isSharded()) { throw MappingException::cannotUseShardedCollectionInOutStage($classMetadata->name); } return parent::out($classMetadata->getCollection()); }
function it_checks_a_non_existent_mapping_relationship($classMetadata, ConfigurationInterface $configuration) { $configuration->getType()->willReturn(ConfigurationInterface::TYPE_MULTI); $configuration->getName()->willReturn('foo'); $classMetadata->getFieldMapping('foo')->willThrow(MappingException::mappingNotFound('spec\\Pim\\Bundle\\ReferenceDataBundle\\RequirementChecker\\CustomValidProductValue', 'foo')); $this->check($configuration)->shouldReturn(false); $this->getFailure()->shouldReturn("No mapping found for field 'foo' in class " . "'spec\\Pim\\Bundle\\ReferenceDataBundle\\RequirementChecker\\CustomValidProductValue'."); }
/** * {@inheritdoc} */ public function loadMetadataForClass($className, ClassMetadata $class) { /** @var $class ClassMetadataInfo */ $reflClass = $class->getReflectionClass(); $classAnnotations = $this->reader->getClassAnnotations($reflClass); $documentAnnots = array(); foreach ($classAnnotations as $annot) { $classAnnotations[get_class($annot)] = $annot; foreach ($this->entityAnnotationClasses as $annotClass => $i) { if ($annot instanceof $annotClass) { $documentAnnots[$i] = $annot; continue 2; } } // non-document class annotations if ($annot instanceof ODM\AbstractIndex) { $this->addIndex($class, $annot); } if ($annot instanceof ODM\Indexes) { foreach (is_array($annot->value) ? $annot->value : array($annot->value) as $index) { $this->addIndex($class, $index); } } elseif ($annot instanceof ODM\InheritanceType) { $class->setInheritanceType(constant('Doctrine\\ODM\\MongoDB\\Mapping\\ClassMetadata::INHERITANCE_TYPE_' . $annot->value)); } elseif ($annot instanceof ODM\DiscriminatorField) { // $fieldName property is deprecated, but fall back for BC if (isset($annot->value)) { $class->setDiscriminatorField($annot->value); } elseif (isset($annot->name)) { $class->setDiscriminatorField($annot->name); } elseif (isset($annot->fieldName)) { $class->setDiscriminatorField($annot->fieldName); } } elseif ($annot instanceof ODM\DiscriminatorMap) { $class->setDiscriminatorMap($annot->value); } elseif ($annot instanceof ODM\DiscriminatorValue) { $class->setDiscriminatorValue($annot->value); } elseif ($annot instanceof ODM\ChangeTrackingPolicy) { $class->setChangeTrackingPolicy(constant('Doctrine\\ODM\\MongoDB\\Mapping\\ClassMetadata::CHANGETRACKING_' . $annot->value)); } } if (!$documentAnnots) { throw MappingException::classIsNotAValidDocument($className); } // find the winning document annotation ksort($documentAnnots); $documentAnnot = reset($documentAnnots); if ($documentAnnot instanceof ODM\MappedSuperclass) { $class->isMappedSuperclass = true; } elseif ($documentAnnot instanceof ODM\EmbeddedDocument) { $class->isEmbeddedDocument = true; } if (isset($documentAnnot->db)) { $class->setDatabase($documentAnnot->db); } if (isset($documentAnnot->collection)) { $class->setCollection($documentAnnot->collection); } if (isset($documentAnnot->repositoryClass)) { $class->setCustomRepositoryClass($documentAnnot->repositoryClass); } if (isset($documentAnnot->indexes)) { foreach ($documentAnnot->indexes as $index) { $this->addIndex($class, $index); } } if (isset($documentAnnot->requireIndexes)) { $class->setRequireIndexes($documentAnnot->requireIndexes); } if (isset($documentAnnot->slaveOkay)) { $class->setSlaveOkay($documentAnnot->slaveOkay); } foreach ($reflClass->getProperties() as $property) { if ($class->isMappedSuperclass && !$property->isPrivate() || $class->isInheritedField($property->name) && $property->getDeclaringClass()->name !== $class->name) { continue; } $indexes = array(); $mapping = array('fieldName' => $property->getName()); $fieldAnnot = null; foreach ($this->reader->getPropertyAnnotations($property) as $annot) { if ($annot instanceof ODM\AbstractField) { $fieldAnnot = $annot; } if ($annot instanceof ODM\AbstractIndex) { $indexes[] = $annot; } if ($annot instanceof ODM\Indexes) { foreach (is_array($annot->value) ? $annot->value : array($annot->value) as $index) { $indexes[] = $index; } } elseif ($annot instanceof ODM\AlsoLoad) { $mapping['alsoLoadFields'] = (array) $annot->value; } elseif ($annot instanceof ODM\Version) { $mapping['version'] = true; } elseif ($annot instanceof ODM\Lock) { $mapping['lock'] = true; } } if ($fieldAnnot) { $mapping = array_replace($mapping, (array) $fieldAnnot); $class->mapField($mapping); } if ($indexes) { foreach ($indexes as $index) { $name = isset($mapping['name']) ? $mapping['name'] : $mapping['fieldName']; $keys = array($name => $index->order ?: 'asc'); $this->addIndex($class, $index, $keys); } } } /** @var $method \ReflectionMethod */ foreach ($reflClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) { /* Filter for the declaring class only. Callbacks from parent * classes will already be registered. */ if ($method->getDeclaringClass()->name !== $reflClass->name) { continue; } foreach ($this->reader->getMethodAnnotations($method) as $annot) { if ($annot instanceof ODM\AlsoLoad) { $class->registerAlsoLoadMethod($method->getName(), $annot->value); } if (!isset($classAnnotations['Doctrine\\ODM\\MongoDB\\Mapping\\Annotations\\HasLifecycleCallbacks'])) { continue; } if ($annot instanceof ODM\PrePersist) { $class->addLifecycleCallback($method->getName(), Events::prePersist); } elseif ($annot instanceof ODM\PostPersist) { $class->addLifecycleCallback($method->getName(), Events::postPersist); } elseif ($annot instanceof ODM\PreUpdate) { $class->addLifecycleCallback($method->getName(), Events::preUpdate); } elseif ($annot instanceof ODM\PostUpdate) { $class->addLifecycleCallback($method->getName(), Events::postUpdate); } elseif ($annot instanceof ODM\PreRemove) { $class->addLifecycleCallback($method->getName(), Events::preRemove); } elseif ($annot instanceof ODM\PostRemove) { $class->addLifecycleCallback($method->getName(), Events::postRemove); } elseif ($annot instanceof ODM\PreLoad) { $class->addLifecycleCallback($method->getName(), Events::preLoad); } elseif ($annot instanceof ODM\PostLoad) { $class->addLifecycleCallback($method->getName(), Events::postLoad); } elseif ($annot instanceof ODM\PreFlush) { $class->addLifecycleCallback($method->getName(), Events::preFlush); } } } }
/** * Gets the mapping of a field. * * @param string $fieldName The field name. * * @return array The field mapping. * * @throws MappingException if the $fieldName is not found in the fieldMappings array * * @throws MappingException */ public function getFieldMapping($fieldName) { if (!isset($this->fieldMappings[$fieldName])) { throw MappingException::mappingNotFound($this->name, $fieldName); } return $this->fieldMappings[$fieldName]; }
/** * Validates the identifier mapping. * * @param ClassMetadata $class */ protected function validateIdentifier($class) { if (!$class->identifier && !$class->isMappedSuperclass && !$class->isEmbeddedDocument) { throw MappingException::identifierRequired($class->name); } }
/** * Gets the field mapping by its DB name. * E.g. it returns identifier's mapping when called with _id. * * @param string $dbFieldName * * @return array * @throws MappingException */ public function getFieldMappingByDbFieldName($dbFieldName) { foreach ($this->fieldMappings as $mapping) { if ($mapping['name'] == $dbFieldName) { return $mapping; } } throw MappingException::mappingNotFoundByDbName($this->name, $dbFieldName); }
/** * Gets reference mapping for current field from current class or its descendants. * * @return array * @throws MappingException */ private function getReferenceMapping() { $mapping = null; try { $mapping = $this->class->getFieldMapping($this->currentField); } catch (MappingException $e) { if (empty($this->class->discriminatorMap)) { throw $e; } $foundIn = null; foreach ($this->class->discriminatorMap as $child) { $childClass = $this->dm->getClassMetadata($child); if ($childClass->hasAssociation($this->currentField)) { if ($mapping !== null && $mapping !== $childClass->getFieldMapping($this->currentField)) { throw MappingException::referenceFieldConflict($this->currentField, $foundIn->name, $childClass->name); } $mapping = $childClass->getFieldMapping($this->currentField); $foundIn = $childClass; } } if ($mapping === null) { throw MappingException::mappingNotFoundInClassNorDescendants($this->class->name, $this->currentField); } } return $mapping; }
/** * Overrides an already defined type to use a different implementation. * * @static * @param string $name * @param string $className * @throws MappingException */ public static function overrideType($name, $className) { if (!isset(self::$typesMap[$name])) { throw MappingException::typeNotFound($name); } self::$typesMap[$name] = $className; }
private function completeIdGeneratorMapping(ClassMetadataInfo $class) { $idGenOptions = $class->generatorOptions; switch ($class->generatorType) { case ClassMetadata::GENERATOR_TYPE_AUTO: $class->setIdGenerator(new \Doctrine\ODM\MongoDB\Id\AutoGenerator()); break; case ClassMetadata::GENERATOR_TYPE_INCREMENT: $incrementGenerator = new \Doctrine\ODM\MongoDB\Id\IncrementGenerator(); if (isset($idGenOptions['key'])) { $incrementGenerator->setKey($idGenOptions['key']); } if (isset($idGenOptions['collection'])) { $incrementGenerator->setCollection($idGenOptions['collection']); } $class->setIdGenerator($incrementGenerator); break; case ClassMetadata::GENERATOR_TYPE_UUID: $uuidGenerator = new \Doctrine\ODM\MongoDB\Id\UuidGenerator(); isset($idGenOptions['salt']) && $uuidGenerator->setSalt($idGenOptions['salt']); $class->setIdGenerator($uuidGenerator); break; case ClassMetadata::GENERATOR_TYPE_ALNUM: $alnumGenerator = new \Doctrine\ODM\MongoDB\Id\AlnumGenerator(); if (isset($idGenOptions['pad'])) { $alnumGenerator->setPad($idGenOptions['pad']); } if (isset($idGenOptions['chars'])) { $alnumGenerator->setChars($idGenOptions['chars']); } elseif (isset($idGenOptions['awkwardSafe'])) { $alnumGenerator->setAwkwardSafeMode($idGenOptions['awkwardSafe']); } $class->setIdGenerator($alnumGenerator); break; case ClassMetadata::GENERATOR_TYPE_CUSTOM: if (empty($idGenOptions['class'])) { throw MappingException::missingIdGeneratorClass($class->name); } $customGenerator = new $idGenOptions['class'](); unset($idGenOptions['class']); if (!$customGenerator instanceof \Doctrine\ODM\MongoDB\Id\AbstractIdGenerator) { throw MappingException::classIsNotAValidGenerator(get_class($customGenerator)); } $methods = get_class_methods($customGenerator); foreach ($idGenOptions as $name => $value) { $method = 'set' . ucfirst($name); if (!in_array($method, $methods)) { throw MappingException::missingGeneratorSetter(get_class($customGenerator), $name); } $customGenerator->{$method}($value); } $class->setIdGenerator($customGenerator); break; case ClassMetadata::GENERATOR_TYPE_NONE: break; default: throw new MappingException("Unknown generator type: " . $class->generatorType); } }
/** * Returns a DBRef array for the supplied document. * * @param mixed $document A document object * @param array $referenceMapping Mapping for the field that references the document * * @throws \InvalidArgumentException * @return array A DBRef array */ public function createDBRef($document, array $referenceMapping = null) { if (!is_object($document)) { throw new \InvalidArgumentException('Cannot create a DBRef, the document is not an object'); } $class = $this->getClassMetadata(get_class($document)); $id = $this->unitOfWork->getDocumentIdentifier($document); if (!$id) { throw new \RuntimeException(sprintf('Cannot create a DBRef for class %s without an identifier. Have you forgotten to persist/merge the document first?', $class->name)); } if ($referenceMapping['storeAs'] === ClassMetadataInfo::REFERENCE_STORE_AS_ID) { if ($class->inheritanceType === ClassMetadataInfo::INHERITANCE_TYPE_SINGLE_COLLECTION) { throw MappingException::simpleReferenceMustNotTargetDiscriminatedDocument($referenceMapping['targetDocument']); } return $class->getDatabaseIdentifierValue($id); } $dbRef = array('$ref' => $class->getCollection(), '$id' => $class->getDatabaseIdentifierValue($id)); if ($referenceMapping['storeAs'] === ClassMetadataInfo::REFERENCE_STORE_AS_DB_REF_WITH_DB) { $dbRef['$db'] = $this->getDocumentDatabase($class->name)->getName(); } /* If the class has a discriminator (field and value), use it. A child * class that is not defined in the discriminator map may only have a * discriminator field and no value, so default to the full class name. */ if (isset($class->discriminatorField)) { $dbRef[$class->discriminatorField] = isset($class->discriminatorValue) ? $class->discriminatorValue : $class->name; } /* Add a discriminator value if the referenced document is not mapped * explicitly to a targetDocument class. */ if ($referenceMapping !== null && !isset($referenceMapping['targetDocument'])) { $discriminatorField = $referenceMapping['discriminatorField']; $discriminatorValue = isset($referenceMapping['discriminatorMap']) ? array_search($class->name, $referenceMapping['discriminatorMap']) : $class->name; /* If the discriminator value was not found in the map, use the full * class name. In the future, it may be preferable to throw an * exception here (perhaps based on some strictness option). * * @see PersistenceBuilder::prepareEmbeddedDocumentValue() */ if ($discriminatorValue === false) { $discriminatorValue = $class->name; } $dbRef[$discriminatorField] = $discriminatorValue; } return $dbRef; }
/** * @param string $fieldName * @return $this * @throws MappingException */ private function fromReference($fieldName) { if (!$this->class->hasReference($fieldName)) { MappingException::referenceMappingNotFound($this->class->name, $fieldName); } $referenceMapping = $this->class->getFieldMapping($fieldName); $targetMapping = $this->dm->getClassMetadata($referenceMapping['targetDocument']); parent::from($targetMapping->getCollection()); if ($referenceMapping['isOwningSide']) { if ($referenceMapping['storeAs'] !== ClassMetadataInfo::REFERENCE_STORE_AS_ID) { throw MappingException::cannotLookupNonIdReference($this->class->name, $fieldName); } $this->foreignField('_id')->localField($referenceMapping['name']); } else { if (isset($referenceMapping['repositoryMethod'])) { throw MappingException::repositoryMethodLookupNotAllowed($this->class->name, $fieldName); } $mappedByMapping = $targetMapping->getFieldMapping($referenceMapping['mappedBy']); if ($mappedByMapping['storeAs'] !== ClassMetadataInfo::REFERENCE_STORE_AS_ID) { throw MappingException::cannotLookupNonIdReference($this->class->name, $fieldName); } $this->localField('_id')->foreignField($mappedByMapping['name']); } return $this; }
/** * Finds a document by its identifier * * @throws LockException * @param string|object $id The identifier * @param int $lockMode * @param int $lockVersion * @return object The document. */ public function find($id, $lockMode = LockMode::NONE, $lockVersion = null) { if ($id === null) { return; } if (is_array($id)) { list($identifierFieldName) = $this->class->getIdentifierFieldNames(); if (!isset($id[$identifierFieldName])) { throw MappingException::missingIdentifierField($this->documentName, $identifierFieldName); } $id = $id[$identifierFieldName]; } // Check identity map first if ($document = $this->uow->tryGetById($id, $this->class->rootDocumentName)) { if ($lockMode != LockMode::NONE) { $this->dm->lock($document, $lockMode, $lockVersion); } return $document; // Hit! } if ($lockMode == LockMode::NONE) { return $this->uow->getDocumentPersister($this->documentName)->load($id); } else { if ($lockMode == LockMode::OPTIMISTIC) { if (!$this->class->isVersioned) { throw LockException::notVersioned($this->documentName); } $document = $this->uow->getDocumentPersister($this->documentName)->load($id); $this->uow->lock($document, $lockMode, $lockVersion); return $document; } else { return $this->uow->getDocumentPersister($this->documentName)->load($id, null, array(), $lockMode); } } }