/** * {@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); } } } }