/** * {@inheritDoc} */ public function readExtendedMetadata(ClassMetadata $meta, array &$config) { $class = $meta->getReflectionClass(); // class annotations if ($annot = $this->reader->getClassAnnotation($class, self::LOGGABLE)) { $config['loggable'] = true; if ($annot->logEntryClass) { if (!class_exists($annot->logEntryClass)) { throw new InvalidMappingException("LogEntry class: {$annot->logEntryClass} does not exist."); } $config['logEntryClass'] = $annot->logEntryClass; } } // property annotations foreach ($class->getProperties() as $property) { if ($meta->isMappedSuperclass && !$property->isPrivate() || $meta->isInheritedField($property->name) || isset($meta->associationMappings[$property->name]['inherited'])) { continue; } // versioned property if ($versioned = $this->reader->getPropertyAnnotation($property, self::VERSIONED)) { $field = $property->getName(); if ($meta->isCollectionValuedAssociation($field)) { throw new InvalidMappingException("Cannot versioned [{$field}] as it is collection in object - {$meta->name}"); } // fields cannot be overrided and throws mapping exception $config['versioned'][] = $field; } } }
/** * {@inheritDoc} */ public function readExtendedMetadata(ClassMetadata $meta, array &$config) { $class = $meta->getReflectionClass(); // property annotations foreach ($class->getProperties() as $property) { if ($meta->isMappedSuperclass && !$property->isPrivate() || $meta->isInheritedField($property->name) || isset($meta->associationMappings[$property->name]['inherited'])) { continue; } if ($timestampable = $this->reader->getPropertyAnnotation($property, self::TIMESTAMPABLE)) { $field = $property->getName(); if (!$meta->hasField($field)) { throw new InvalidMappingException("Unable to find timestampable [{$field}] as mapped property in entity - {$meta->name}"); } if (!$this->isValidField($meta, $field)) { throw new InvalidMappingException("Field - [{$field}] type is not valid and must be 'date', 'datetime' or 'time' in class - {$meta->name}"); } if (!in_array($timestampable->on, array('update', 'create', 'change'))) { throw new InvalidMappingException("Field - [{$field}] trigger 'on' is not one of [update, create, change] in class - {$meta->name}"); } if ($timestampable->on == 'change') { if (!isset($timestampable->field) || !isset($timestampable->value)) { throw new InvalidMappingException("Missing parameters on property - {$field}, field and value must be set on [change] trigger in class - {$meta->name}"); } $field = array('field' => $field, 'trackedField' => $timestampable->field, 'value' => $timestampable->value); } // properties are unique and mapper checks that, no risk here $config[$timestampable->on][] = $field; } } }
/** * {@inheritDoc} */ public function readExtendedMetadata(ClassMetadata $meta, array &$config) { $class = $meta->getReflectionClass(); // property annotations foreach ($class->getProperties() as $property) { if ($meta->isMappedSuperclass && !$property->isPrivate() || $meta->isInheritedField($property->name) || isset($meta->associationMappings[$property->name]['inherited'])) { continue; } // position if ($position = $this->reader->getPropertyAnnotation($property, self::POSITION)) { $field = $property->getName(); if (!$meta->hasField($field)) { throw new InvalidMappingException("Unable to find 'position' - [{$field}] as mapped property in entity - {$meta->name}"); } if (!$this->isValidField($meta, $field)) { throw new InvalidMappingException("Sortable position field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}"); } $config['position'] = $field; } // group if ($group = $this->reader->getPropertyAnnotation($property, self::GROUP)) { $field = $property->getName(); if (!$meta->hasField($field) && !$meta->hasAssociation($field)) { throw new InvalidMappingException("Unable to find 'group' - [{$field}] as mapped property in entity - {$meta->name}"); } if (!isset($config['groups'])) { $config['groups'] = array(); } $config['groups'][] = $field; } } }
/** * {@inheritDoc} */ public function readExtendedMetadata(ClassMetadata $meta, array &$config) { $class = $meta->getReflectionClass(); // property annotations $config['fields'] = array(); $config['fields_delete'] = array(); foreach ($class->getProperties() as $property) { if ($meta->isMappedSuperclass && !$property->isPrivate() || $meta->isInheritedField($property->name) || isset($meta->associationMappings[$property->name]['inherited'])) { continue; } $field = null; if ($file = $this->reader->getPropertyAnnotation($property, self::FILE)) { $field['name'] = $property->getName(); $field['dir'] = CMSCore::init()->getUploadsDir() . '/' . $file->dir; if (!$meta->hasField($field['name'])) { throw new InvalidMappingException("Unable to find timestampable [{$field}] as mapped property in entity - {$meta->name}"); } } // if ($fileDelete = $this->reader->getPropertyAnnotation($property, self::FILE_DELETE)) { // // $config['fields_delete'][] = $property->getName(); // // } if ($field) { $config['fields'][] = $field; } } }
public function readExtendedMetadata(ClassMetadata $meta, array &$config) { // load our available annotations require_once __DIR__ . '/../Annotations.php'; $reader = new AnnotationReader(); // set annotation namespace and alias //$reader->setAnnotationNamespaceAlias('Gedmo\Mapping\Mock\Extension\Encoder\Mapping\\', 'ext'); $class = $meta->getReflectionClass(); // check only property annotations foreach ($class->getProperties() as $property) { // skip inherited properties if ($meta->isMappedSuperclass && !$property->isPrivate() || $meta->isInheritedField($property->name) || isset($meta->associationMappings[$property->name]['inherited'])) { continue; } // now lets check if property has our annotation if ($encode = $reader->getPropertyAnnotation($property, 'Gedmo\\Mapping\\Mock\\Extension\\Encoder\\Mapping\\Encode')) { $field = $property->getName(); // check if field is mapped if (!$meta->hasField($field)) { throw new \Exception("Field is not mapped as object property"); } // allow encoding only strings if (!in_array($encode->type, array('sha1', 'md5'))) { throw new \Exception("Invalid encoding type supplied"); } // validate encoding type $mapping = $meta->getFieldMapping($field); if ($mapping['type'] != 'string') { throw new \Exception("Only strings can be encoded"); } // store the metadata $config['encode'][$field] = array('type' => $encode->type, 'secret' => $encode->secret); } } }
/** * {@inheritDoc} */ public function readExtendedMetadata(ClassMetadata $meta, array &$config) { $class = $meta->getReflectionClass(); // property annotations foreach ($class->getProperties() as $property) { if ($meta->isMappedSuperclass && !$property->isPrivate() || $meta->isInheritedField($property->name) || isset($meta->associationMappings[$property->name]['inherited'])) { continue; } // slug property if ($slug = $this->reader->getPropertyAnnotation($property, self::SLUG)) { $field = $property->getName(); if (!$meta->hasField($field)) { throw new InvalidMappingException("Unable to find slug [{$field}] as mapped property in entity - {$meta->name}"); } if (!$this->isValidField($meta, $field)) { throw new InvalidMappingException("Cannot use field - [{$field}] for slug storage, type is not valid and must be 'string' or 'text' in class - {$meta->name}"); } // process slug handlers $handlers = array(); if (is_array($slug->handlers) && $slug->handlers) { foreach ($slug->handlers as $handler) { if (!$handler instanceof SlugHandler) { throw new InvalidMappingException("SlugHandler: {$handler} should be instance of SlugHandler annotation in entity - {$meta->name}"); } if (!strlen($handler->class)) { throw new InvalidMappingException("SlugHandler class: {$handler->class} should be a valid class name in entity - {$meta->name}"); } $class = $handler->class; $handlers[$class] = array(); foreach ((array) $handler->options as $option) { if (!$option instanceof SlugHandlerOption) { throw new InvalidMappingException("SlugHandlerOption: {$option} should be instance of SlugHandlerOption annotation in entity - {$meta->name}"); } if (!strlen($option->name)) { throw new InvalidMappingException("SlugHandlerOption name: {$option->name} should be valid name in entity - {$meta->name}"); } $handlers[$class][$option->name] = $option->value; } $class::validate($handlers[$class], $meta); } } // process slug fields if (empty($slug->fields) || !is_array($slug->fields)) { throw new InvalidMappingException("Slug must contain at least one field for slug generation in class - {$meta->name}"); } foreach ($slug->fields as $slugField) { if (!$meta->hasField($slugField)) { throw new InvalidMappingException("Unable to find slug [{$slugField}] as mapped property in entity - {$meta->name}"); } if (!$this->isValidField($meta, $slugField)) { throw new InvalidMappingException("Cannot use field - [{$slugField}] for slug storage, type is not valid and must be 'string' or 'text' in class - {$meta->name}"); } } // set all options $config['slugs'][$field] = array('fields' => $slug->fields, 'slug' => $field, 'style' => $slug->style, 'updatable' => $slug->updatable, 'unique' => $slug->unique, 'separator' => $slug->separator, 'handlers' => $handlers); } } }
/** * {@inheritdoc} */ protected function loadExtendedClassMetadata(ClassMetadata $baseClassMetadata, ClassMetadataInterface $extendedClassMetadata) { $classReflection = $extendedClassMetadata->getClassReflection(); foreach ($classReflection->getProperties() as $property) { if ($baseClassMetadata->isMappedSuperclass && !$property->isPrivate() || $baseClassMetadata->isInheritedField($property->name) || isset($baseClassMetadata->associationMappings[$property->name]['inherited'])) { continue; } if ($uploadableAnnotation = $this->getAnnotationReader()->getPropertyAnnotation($property, self::UPLOADABLE)) { $extendedClassMetadata->addUploadableProperty($property->getName(), $uploadableAnnotation->targetField, $uploadableAnnotation->filesystem, $uploadableAnnotation->keymaker, $uploadableAnnotation->keyLength, $uploadableAnnotation->keyPattern); } } }
/** * {@inheritDoc} */ public function readExtendedMetadata(ClassMetadata $meta, array &$config) { $class = $meta->getReflectionClass(); // property annotations foreach ($class->getProperties() as $property) { if ($meta->isMappedSuperclass && !$property->isPrivate() || $meta->isInheritedField($property->name) || isset($meta->associationMappings[$property->name]['inherited'])) { continue; } if ($demo = $this->reader->getPropertyAnnotation($property, self::DEMO)) { $field = $property->getName(); $value = $demo->text; if (!$meta->hasField($field)) { throw new InvalidMappingException("Unable to find timestampable [{$field}] as mapped property in entity - {$meta->name}"); } $config['text'][] = array('field' => $field, 'value' => $value); } } }
/** * {@inheritDoc} */ protected function loadExtendedClassMetadata(ClassMetadata $baseClassMetadata, ClassMetadataInterface $extendedClassMetadata) { $classReflection = $extendedClassMetadata->getClassReflection(); foreach ($classReflection->getProperties() as $property) { if ($baseClassMetadata->isMappedSuperclass && !$property->isPrivate() || $baseClassMetadata->isInheritedField($property->name) || isset($baseClassMetadata->associationMappings[$property->name]['inherited'])) { continue; } if ($translatableAnnotation = $this->getAnnotationReader()->getPropertyAnnotation($property, self::TRANSLATABLE)) { if (!isset($translatableAnnotation->mappedBy)) { throw new AnnotationException('Annotation \'Translatable\' in property \'' . $property . '\' of class \'' . $baseClassMetadata->name . '\' does not have required \'mappedBy\' attribute'); } $extendedClassMetadata->addTranslatableProperty($translatableAnnotation->mappedBy, $property->getName(), $translatableAnnotation->targetField); } if ($languageAnnotation = $this->getAnnotationReader()->getPropertyAnnotation($property, self::LOCALE)) { $extendedClassMetadata->localeProperty = $property->getName(); } } }
/** * {@inheritDoc} */ public function readExtendedMetadata(ClassMetadata $meta, array &$config) { $class = $meta->getReflectionClass(); // class annotations if ($annot = $this->reader->getClassAnnotation($class, self::ENTITY_CLASS)) { if (!class_exists($annot->class)) { throw new InvalidMappingException("Translation class: {$annot->class} does not exist."); } $config['translationClass'] = $annot->class; } // property annotations foreach ($class->getProperties() as $property) { if ($meta->isMappedSuperclass && !$property->isPrivate() || $meta->isInheritedField($property->name) || isset($meta->associationMappings[$property->name]['inherited'])) { continue; } // translatable property if ($translatable = $this->reader->getPropertyAnnotation($property, self::TRANSLATABLE)) { $field = $property->getName(); if (!$meta->hasField($field)) { throw new InvalidMappingException("Unable to find translatable [{$field}] as mapped property in entity - {$meta->name}"); } // fields cannot be overrided and throws mapping exception $config['fields'][] = $field; } // locale property if ($locale = $this->reader->getPropertyAnnotation($property, self::LOCALE)) { $field = $property->getName(); if ($meta->hasField($field)) { throw new InvalidMappingException("Locale field [{$field}] should not be mapped as column property in entity - {$meta->name}, since it makes no sence"); } $config['locale'] = $field; } elseif ($language = $this->reader->getPropertyAnnotation($property, self::LANGUAGE)) { $field = $property->getName(); if ($meta->hasField($field)) { throw new InvalidMappingException("Language field [{$field}] should not be mapped as column property in entity - {$meta->name}, since it makes no sence"); } $config['locale'] = $field; } } }
/** * {@inheritDoc} */ public function readExtendedMetadata(ClassMetadata $meta, array &$config) { $class = $meta->getReflectionClass(); // property annotations foreach ($class->getProperties() as $property) { if ($meta->isMappedSuperclass && !$property->isPrivate() || $meta->isInheritedField($property->name) || isset($meta->associationMappings[$property->name]['inherited'])) { continue; } // sluggable property if ($sluggable = $this->reader->getPropertyAnnotation($property, self::SLUGGABLE)) { $field = $property->getName(); if (!$meta->hasField($field)) { throw new InvalidMappingException("Unable to find sluggable [{$field}] as mapped property in entity - {$meta->name}"); } if (!$this->isValidField($meta, $field)) { throw new InvalidMappingException("Cannot slug field - [{$field}] type is not valid and must be 'string' in class - {$meta->name}"); } if (!is_null($sluggable->slugField) and !$meta->hasField($sluggable->slugField)) { throw new InvalidMappingException("Unable to find slug [{$field}] as mapped property in entity - {$meta->name}"); } $config['fields'][$sluggable->slugField][] = array('field' => $field, 'position' => $sluggable->position, 'slugField' => $sluggable->slugField); } // slug property if ($slug = $this->reader->getPropertyAnnotation($property, self::SLUG)) { $field = $property->getName(); if (!$meta->hasField($field)) { throw new InvalidMappingException("Unable to find slug [{$field}] as mapped property in entity - {$meta->name}"); } if (!$this->isValidField($meta, $field)) { throw new InvalidMappingException("Cannot use field - [{$field}] for slug storage, type is not valid and must be 'string' in class - {$meta->name}"); } $config['slugFields'][$field]['slug'] = $field; $config['slugFields'][$field]['style'] = $slug->style; $config['slugFields'][$field]['updatable'] = $slug->updatable; $config['slugFields'][$field]['unique'] = $slug->unique; $config['slugFields'][$field]['separator'] = $slug->separator; } } }
/** * {@inheritDoc} */ public function loadMetadataForClass($className, ClassMetadata $metadata) { /* @var $metadata \Doctrine\ORM\Mapping\ClassMetadataInfo */ $class = $metadata->getReflectionClass(); if (!$class) { // this happens when running annotation driver in combination with // static reflection services. This is not the nicest fix $class = new \ReflectionClass($metadata->name); } $classAnnotations = $this->reader->getClassAnnotations($class); if ($classAnnotations) { foreach ($classAnnotations as $key => $annot) { if (!is_numeric($key)) { continue; } $classAnnotations[get_class($annot)] = $annot; } } // Evaluate Entity annotation if (isset($classAnnotations['Doctrine\\ORM\\Mapping\\Entity'])) { $entityAnnot = $classAnnotations['Doctrine\\ORM\\Mapping\\Entity']; if ($entityAnnot->repositoryClass !== null) { $metadata->setCustomRepositoryClass($entityAnnot->repositoryClass); } if ($entityAnnot->readOnly) { $metadata->markReadOnly(); } } else { if (isset($classAnnotations['Doctrine\\ORM\\Mapping\\MappedSuperclass'])) { $mappedSuperclassAnnot = $classAnnotations['Doctrine\\ORM\\Mapping\\MappedSuperclass']; $metadata->setCustomRepositoryClass($mappedSuperclassAnnot->repositoryClass); $metadata->isMappedSuperclass = true; } else { if (isset($classAnnotations['Doctrine\\ORM\\Mapping\\Embeddable'])) { $metadata->isEmbeddedClass = true; } else { throw MappingException::classIsNotAValidEntityOrMappedSuperClass($className); } } } // Evaluate Table annotation if (isset($classAnnotations['Doctrine\\ORM\\Mapping\\Table'])) { $tableAnnot = $classAnnotations['Doctrine\\ORM\\Mapping\\Table']; $primaryTable = array('name' => $tableAnnot->name, 'schema' => $tableAnnot->schema); if ($tableAnnot->indexes !== null) { foreach ($tableAnnot->indexes as $indexAnnot) { $index = array('columns' => $indexAnnot->columns); if (!empty($indexAnnot->flags)) { $index['flags'] = $indexAnnot->flags; } if (!empty($indexAnnot->options)) { $index['options'] = $indexAnnot->options; } if (!empty($indexAnnot->name)) { $primaryTable['indexes'][$indexAnnot->name] = $index; } else { $primaryTable['indexes'][] = $index; } } } if ($tableAnnot->uniqueConstraints !== null) { foreach ($tableAnnot->uniqueConstraints as $uniqueConstraintAnnot) { $uniqueConstraint = array('columns' => $uniqueConstraintAnnot->columns); if (!empty($uniqueConstraintAnnot->options)) { $uniqueConstraint['options'] = $uniqueConstraintAnnot->options; } if (!empty($uniqueConstraintAnnot->name)) { $primaryTable['uniqueConstraints'][$uniqueConstraintAnnot->name] = $uniqueConstraint; } else { $primaryTable['uniqueConstraints'][] = $uniqueConstraint; } } } if ($tableAnnot->options) { $primaryTable['options'] = $tableAnnot->options; } $metadata->setPrimaryTable($primaryTable); } // Evaluate @Cache annotation if (isset($classAnnotations['Doctrine\\ORM\\Mapping\\Cache'])) { $cacheAnnot = $classAnnotations['Doctrine\\ORM\\Mapping\\Cache']; $cacheMap = array('region' => $cacheAnnot->region, 'usage' => constant('Doctrine\\ORM\\Mapping\\ClassMetadata::CACHE_USAGE_' . $cacheAnnot->usage)); $metadata->enableCache($cacheMap); } // Evaluate NamedNativeQueries annotation if (isset($classAnnotations['Doctrine\\ORM\\Mapping\\NamedNativeQueries'])) { $namedNativeQueriesAnnot = $classAnnotations['Doctrine\\ORM\\Mapping\\NamedNativeQueries']; foreach ($namedNativeQueriesAnnot->value as $namedNativeQuery) { $metadata->addNamedNativeQuery(array('name' => $namedNativeQuery->name, 'query' => $namedNativeQuery->query, 'resultClass' => $namedNativeQuery->resultClass, 'resultSetMapping' => $namedNativeQuery->resultSetMapping)); } } // Evaluate SqlResultSetMappings annotation if (isset($classAnnotations['Doctrine\\ORM\\Mapping\\SqlResultSetMappings'])) { $sqlResultSetMappingsAnnot = $classAnnotations['Doctrine\\ORM\\Mapping\\SqlResultSetMappings']; foreach ($sqlResultSetMappingsAnnot->value as $resultSetMapping) { $entities = array(); $columns = array(); foreach ($resultSetMapping->entities as $entityResultAnnot) { $entityResult = array('fields' => array(), 'entityClass' => $entityResultAnnot->entityClass, 'discriminatorColumn' => $entityResultAnnot->discriminatorColumn); foreach ($entityResultAnnot->fields as $fieldResultAnnot) { $entityResult['fields'][] = array('name' => $fieldResultAnnot->name, 'column' => $fieldResultAnnot->column); } $entities[] = $entityResult; } foreach ($resultSetMapping->columns as $columnResultAnnot) { $columns[] = array('name' => $columnResultAnnot->name); } $metadata->addSqlResultSetMapping(array('name' => $resultSetMapping->name, 'entities' => $entities, 'columns' => $columns)); } } // Evaluate NamedQueries annotation if (isset($classAnnotations['Doctrine\\ORM\\Mapping\\NamedQueries'])) { $namedQueriesAnnot = $classAnnotations['Doctrine\\ORM\\Mapping\\NamedQueries']; if (!is_array($namedQueriesAnnot->value)) { throw new \UnexpectedValueException("@NamedQueries should contain an array of @NamedQuery annotations."); } foreach ($namedQueriesAnnot->value as $namedQuery) { if (!$namedQuery instanceof \Doctrine\ORM\Mapping\NamedQuery) { throw new \UnexpectedValueException("@NamedQueries should contain an array of @NamedQuery annotations."); } $metadata->addNamedQuery(array('name' => $namedQuery->name, 'query' => $namedQuery->query)); } } // Evaluate InheritanceType annotation if (isset($classAnnotations['Doctrine\\ORM\\Mapping\\InheritanceType'])) { $inheritanceTypeAnnot = $classAnnotations['Doctrine\\ORM\\Mapping\\InheritanceType']; $metadata->setInheritanceType(constant('Doctrine\\ORM\\Mapping\\ClassMetadata::INHERITANCE_TYPE_' . $inheritanceTypeAnnot->value)); if ($metadata->inheritanceType != \Doctrine\ORM\Mapping\ClassMetadata::INHERITANCE_TYPE_NONE) { // Evaluate DiscriminatorColumn annotation if (isset($classAnnotations['Doctrine\\ORM\\Mapping\\DiscriminatorColumn'])) { $discrColumnAnnot = $classAnnotations['Doctrine\\ORM\\Mapping\\DiscriminatorColumn']; $metadata->setDiscriminatorColumn(array('name' => $discrColumnAnnot->name, 'type' => $discrColumnAnnot->type ?: 'string', 'length' => $discrColumnAnnot->length ?: 255, 'columnDefinition' => $discrColumnAnnot->columnDefinition)); } else { $metadata->setDiscriminatorColumn(array('name' => 'dtype', 'type' => 'string', 'length' => 255)); } // Evaluate DiscriminatorMap annotation if (isset($classAnnotations['Doctrine\\ORM\\Mapping\\DiscriminatorMap'])) { $discrMapAnnot = $classAnnotations['Doctrine\\ORM\\Mapping\\DiscriminatorMap']; $metadata->setDiscriminatorMap($discrMapAnnot->value); } } } // Evaluate DoctrineChangeTrackingPolicy annotation if (isset($classAnnotations['Doctrine\\ORM\\Mapping\\ChangeTrackingPolicy'])) { $changeTrackingAnnot = $classAnnotations['Doctrine\\ORM\\Mapping\\ChangeTrackingPolicy']; $metadata->setChangeTrackingPolicy(constant('Doctrine\\ORM\\Mapping\\ClassMetadata::CHANGETRACKING_' . $changeTrackingAnnot->value)); } // Evaluate annotations on properties/fields /* @var $property \ReflectionProperty */ foreach ($class->getProperties() as $property) { if ($metadata->isMappedSuperclass && !$property->isPrivate() || $metadata->isInheritedField($property->name) || $metadata->isInheritedAssociation($property->name) || $metadata->isInheritedEmbeddedClass($property->name)) { continue; } $mapping = array(); $mapping['fieldName'] = $property->getName(); // Evaluate @Cache annotation if (($cacheAnnot = $this->reader->getPropertyAnnotation($property, 'Doctrine\\ORM\\Mapping\\Cache')) !== null) { $mapping['cache'] = $metadata->getAssociationCacheDefaults($mapping['fieldName'], array('usage' => constant('Doctrine\\ORM\\Mapping\\ClassMetadata::CACHE_USAGE_' . $cacheAnnot->usage), 'region' => $cacheAnnot->region)); } // Check for JoinColumn/JoinColumns annotations $joinColumns = array(); if ($joinColumnAnnot = $this->reader->getPropertyAnnotation($property, 'Doctrine\\ORM\\Mapping\\JoinColumn')) { $joinColumns[] = $this->joinColumnToArray($joinColumnAnnot); } else { if ($joinColumnsAnnot = $this->reader->getPropertyAnnotation($property, 'Doctrine\\ORM\\Mapping\\JoinColumns')) { foreach ($joinColumnsAnnot->value as $joinColumn) { $joinColumns[] = $this->joinColumnToArray($joinColumn); } } } // Field can only be annotated with one of: // @Column, @OneToOne, @OneToMany, @ManyToOne, @ManyToMany if ($columnAnnot = $this->reader->getPropertyAnnotation($property, 'Doctrine\\ORM\\Mapping\\Column')) { if ($columnAnnot->type == null) { throw MappingException::propertyTypeIsRequired($className, $property->getName()); } $mapping = $this->columnToArray($property->getName(), $columnAnnot); if ($idAnnot = $this->reader->getPropertyAnnotation($property, 'Doctrine\\ORM\\Mapping\\Id')) { $mapping['id'] = true; } if ($generatedValueAnnot = $this->reader->getPropertyAnnotation($property, 'Doctrine\\ORM\\Mapping\\GeneratedValue')) { $metadata->setIdGeneratorType(constant('Doctrine\\ORM\\Mapping\\ClassMetadata::GENERATOR_TYPE_' . $generatedValueAnnot->strategy)); } if ($this->reader->getPropertyAnnotation($property, 'Doctrine\\ORM\\Mapping\\Version')) { $metadata->setVersionMapping($mapping); } $metadata->mapField($mapping); // Check for SequenceGenerator/TableGenerator definition if ($seqGeneratorAnnot = $this->reader->getPropertyAnnotation($property, 'Doctrine\\ORM\\Mapping\\SequenceGenerator')) { $metadata->setSequenceGeneratorDefinition(array('sequenceName' => $seqGeneratorAnnot->sequenceName, 'allocationSize' => $seqGeneratorAnnot->allocationSize, 'initialValue' => $seqGeneratorAnnot->initialValue)); } else { if ($this->reader->getPropertyAnnotation($property, 'Doctrine\\ORM\\Mapping\\TableGenerator')) { throw MappingException::tableIdGeneratorNotImplemented($className); } else { if ($customGeneratorAnnot = $this->reader->getPropertyAnnotation($property, 'Doctrine\\ORM\\Mapping\\CustomIdGenerator')) { $metadata->setCustomGeneratorDefinition(array('class' => $customGeneratorAnnot->class)); } } } } else { if ($oneToOneAnnot = $this->reader->getPropertyAnnotation($property, 'Doctrine\\ORM\\Mapping\\OneToOne')) { if ($idAnnot = $this->reader->getPropertyAnnotation($property, 'Doctrine\\ORM\\Mapping\\Id')) { $mapping['id'] = true; } $mapping['targetEntity'] = $oneToOneAnnot->targetEntity; $mapping['joinColumns'] = $joinColumns; $mapping['mappedBy'] = $oneToOneAnnot->mappedBy; $mapping['inversedBy'] = $oneToOneAnnot->inversedBy; $mapping['cascade'] = $oneToOneAnnot->cascade; $mapping['orphanRemoval'] = $oneToOneAnnot->orphanRemoval; $mapping['fetch'] = $this->getFetchMode($className, $oneToOneAnnot->fetch); $metadata->mapOneToOne($mapping); } else { if ($oneToManyAnnot = $this->reader->getPropertyAnnotation($property, 'Doctrine\\ORM\\Mapping\\OneToMany')) { $mapping['mappedBy'] = $oneToManyAnnot->mappedBy; $mapping['targetEntity'] = $oneToManyAnnot->targetEntity; $mapping['cascade'] = $oneToManyAnnot->cascade; $mapping['indexBy'] = $oneToManyAnnot->indexBy; $mapping['orphanRemoval'] = $oneToManyAnnot->orphanRemoval; $mapping['fetch'] = $this->getFetchMode($className, $oneToManyAnnot->fetch); if ($orderByAnnot = $this->reader->getPropertyAnnotation($property, 'Doctrine\\ORM\\Mapping\\OrderBy')) { $mapping['orderBy'] = $orderByAnnot->value; } $metadata->mapOneToMany($mapping); } else { if ($manyToOneAnnot = $this->reader->getPropertyAnnotation($property, 'Doctrine\\ORM\\Mapping\\ManyToOne')) { if ($idAnnot = $this->reader->getPropertyAnnotation($property, 'Doctrine\\ORM\\Mapping\\Id')) { $mapping['id'] = true; } $mapping['joinColumns'] = $joinColumns; $mapping['cascade'] = $manyToOneAnnot->cascade; $mapping['inversedBy'] = $manyToOneAnnot->inversedBy; $mapping['targetEntity'] = $manyToOneAnnot->targetEntity; $mapping['fetch'] = $this->getFetchMode($className, $manyToOneAnnot->fetch); $metadata->mapManyToOne($mapping); } else { if ($manyToManyAnnot = $this->reader->getPropertyAnnotation($property, 'Doctrine\\ORM\\Mapping\\ManyToMany')) { $joinTable = array(); if ($joinTableAnnot = $this->reader->getPropertyAnnotation($property, 'Doctrine\\ORM\\Mapping\\JoinTable')) { $joinTable = array('name' => $joinTableAnnot->name, 'schema' => $joinTableAnnot->schema); foreach ($joinTableAnnot->joinColumns as $joinColumn) { $joinTable['joinColumns'][] = $this->joinColumnToArray($joinColumn); } foreach ($joinTableAnnot->inverseJoinColumns as $joinColumn) { $joinTable['inverseJoinColumns'][] = $this->joinColumnToArray($joinColumn); } } $mapping['joinTable'] = $joinTable; $mapping['targetEntity'] = $manyToManyAnnot->targetEntity; $mapping['mappedBy'] = $manyToManyAnnot->mappedBy; $mapping['inversedBy'] = $manyToManyAnnot->inversedBy; $mapping['cascade'] = $manyToManyAnnot->cascade; $mapping['indexBy'] = $manyToManyAnnot->indexBy; $mapping['orphanRemoval'] = $manyToManyAnnot->orphanRemoval; $mapping['fetch'] = $this->getFetchMode($className, $manyToManyAnnot->fetch); if ($orderByAnnot = $this->reader->getPropertyAnnotation($property, 'Doctrine\\ORM\\Mapping\\OrderBy')) { $mapping['orderBy'] = $orderByAnnot->value; } $metadata->mapManyToMany($mapping); } else { if ($embeddedAnnot = $this->reader->getPropertyAnnotation($property, 'Doctrine\\ORM\\Mapping\\Embedded')) { $mapping['class'] = $embeddedAnnot->class; $mapping['columnPrefix'] = $embeddedAnnot->columnPrefix; $metadata->mapEmbedded($mapping); } } } } } } } // Evaluate AssociationOverrides annotation if (isset($classAnnotations['Doctrine\\ORM\\Mapping\\AssociationOverrides'])) { $associationOverridesAnnot = $classAnnotations['Doctrine\\ORM\\Mapping\\AssociationOverrides']; foreach ($associationOverridesAnnot->value as $associationOverride) { $override = array(); $fieldName = $associationOverride->name; // Check for JoinColumn/JoinColumns annotations if ($associationOverride->joinColumns) { $joinColumns = array(); foreach ($associationOverride->joinColumns as $joinColumn) { $joinColumns[] = $this->joinColumnToArray($joinColumn); } $override['joinColumns'] = $joinColumns; } // Check for JoinTable annotations if ($associationOverride->joinTable) { $joinTableAnnot = $associationOverride->joinTable; $joinTable = array('name' => $joinTableAnnot->name, 'schema' => $joinTableAnnot->schema); foreach ($joinTableAnnot->joinColumns as $joinColumn) { $joinTable['joinColumns'][] = $this->joinColumnToArray($joinColumn); } foreach ($joinTableAnnot->inverseJoinColumns as $joinColumn) { $joinTable['inverseJoinColumns'][] = $this->joinColumnToArray($joinColumn); } $override['joinTable'] = $joinTable; } // Check for inversedBy if ($associationOverride->inversedBy) { $override['inversedBy'] = $associationOverride->inversedBy; } $metadata->setAssociationOverride($fieldName, $override); } } // Evaluate AttributeOverrides annotation if (isset($classAnnotations['Doctrine\\ORM\\Mapping\\AttributeOverrides'])) { $attributeOverridesAnnot = $classAnnotations['Doctrine\\ORM\\Mapping\\AttributeOverrides']; foreach ($attributeOverridesAnnot->value as $attributeOverrideAnnot) { $attributeOverride = $this->columnToArray($attributeOverrideAnnot->name, $attributeOverrideAnnot->column); $metadata->setAttributeOverride($attributeOverrideAnnot->name, $attributeOverride); } } // Evaluate EntityListeners annotation if (isset($classAnnotations['Doctrine\\ORM\\Mapping\\EntityListeners'])) { $entityListenersAnnot = $classAnnotations['Doctrine\\ORM\\Mapping\\EntityListeners']; foreach ($entityListenersAnnot->value as $item) { $listenerClassName = $metadata->fullyQualifiedClassName($item); if (!class_exists($listenerClassName)) { throw MappingException::entityListenerClassNotFound($listenerClassName, $className); } $hasMapping = false; $listenerClass = new \ReflectionClass($listenerClassName); /* @var $method \ReflectionMethod */ foreach ($listenerClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) { // find method callbacks. $callbacks = $this->getMethodCallbacks($method); $hasMapping = $hasMapping ?: !empty($callbacks); foreach ($callbacks as $value) { $metadata->addEntityListener($value[1], $listenerClassName, $value[0]); } } // Evaluate the listener using naming convention. if (!$hasMapping) { EntityListenerBuilder::bindEntityListener($metadata, $listenerClassName); } } } // Evaluate @HasLifecycleCallbacks annotation if (isset($classAnnotations['Doctrine\\ORM\\Mapping\\HasLifecycleCallbacks'])) { /* @var $method \ReflectionMethod */ foreach ($class->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) { foreach ($this->getMethodCallbacks($method) as $value) { $metadata->addLifecycleCallback($value[0], $value[1]); } } } }
/** * {@inheritDoc} */ public function readExtendedMetadata(ClassMetadata $meta, array &$config) { $class = $meta->getReflectionClass(); // property annotations foreach ($class->getProperties() as $property) { if ($meta->isMappedSuperclass && !$property->isPrivate() || $meta->isInheritedField($property->name) || isset($meta->associationMappings[$property->name]['inherited']) ) { continue; } // sluggable property if ($sluggable = $this->reader->getPropertyAnnotation($property, self::SLUGGABLE)) { $field = $property->getName(); if (!$meta->hasField($field)) { throw new InvalidMappingException("Unable to find sluggable [{$field}] as mapped property in entity - {$meta->name}"); } if (!$this->isValidField($meta, $field)) { throw new InvalidMappingException("Cannot slug field - [{$field}] type is not valid and must be 'string' in class - {$meta->name}"); } if (!is_null($sluggable->slugField) and !$meta->hasField($sluggable->slugField)) { throw new InvalidMappingException(sprintf('The "%s" property - which is defined as the "slugField" for the "%s" property - does not exist or is not mapped to Doctrine in "%s"', $sluggable->slugField, $field, $meta->name)); } $config['fields'][$sluggable->slugField][] = array('field' => $field, 'position' => $sluggable->position, 'slugField' => $sluggable->slugField); } // slug property if ($slug = $this->reader->getPropertyAnnotation($property, self::SLUG)) { $field = $property->getName(); if (!$meta->hasField($field)) { throw new InvalidMappingException("Unable to find slug [{$field}] as mapped property in entity - {$meta->name}"); } if (!$this->isValidField($meta, $field)) { throw new InvalidMappingException("Cannot use field - [{$field}] for slug storage, type is not valid and must be 'string' in class - {$meta->name}"); } // process slug handlers if (is_array($slug->handlers) && $slug->handlers) { foreach ($slug->handlers as $handler) { if (!$handler instanceof SlugHandler) { throw new InvalidMappingException("SlugHandler: {$handler} should be instance of SlugHandler annotation in entity - {$meta->name}"); } if (!strlen($handler->class)) { throw new InvalidMappingException("SlugHandler class: {$handler->class} should be a valid class name in entity - {$meta->name}"); } $class = $handler->class; $config['handlers'][$class] = array(); foreach ((array)$handler->options as $option) { if (!$option instanceof SlugHandlerOption) { throw new InvalidMappingException("SlugHandlerOption: {$option} should be instance of SlugHandlerOption annotation in entity - {$meta->name}"); } if (!strlen($option->name)) { throw new InvalidMappingException("SlugHandlerOption name: {$option->name} should be valid name in entity - {$meta->name}"); } $config['handlers'][$class][$option->name] = $option->value; } $class::validate($config['handlers'][$class], $meta); } } $config['slugFields'][$field]['slug'] = $field; $config['slugFields'][$field]['style'] = $slug->style; $config['slugFields'][$field]['updatable'] = $slug->updatable; $config['slugFields'][$field]['unique'] = $slug->unique; $config['slugFields'][$field]['separator'] = $slug->separator; } } }
/** * {@inheritdoc} */ public function loadMetadataForClass($className, ClassMetadata $metadata) { /** @var $metadata \Doctrine\ODM\PHPCR\Mapping\ClassMetadata */ $reflClass = $metadata->getReflectionClass(); $documentAnnots = array(); foreach ($this->reader->getClassAnnotations($reflClass) as $annot) { foreach ($this->entityAnnotationClasses as $annotClass => $i) { if ($annot instanceof $annotClass) { $documentAnnots[$i] = $annot; } } } if (!$documentAnnots) { throw MappingException::classIsNotAValidDocument($className); } // find the winning document annotation ksort($documentAnnots); $documentAnnot = reset($documentAnnots); if ($documentAnnot instanceof ODM\MappedSuperclass) { $metadata->isMappedSuperclass = true; } if (null !== $documentAnnot->referenceable) { $metadata->setReferenceable($documentAnnot->referenceable); } if (null !== $documentAnnot->versionable) { $metadata->setVersioned($documentAnnot->versionable); } if (null !== $documentAnnot->mixins) { $metadata->setMixins($documentAnnot->mixins); } if (null !== $documentAnnot->inheritMixins) { $metadata->setInheritMixins($documentAnnot->inheritMixins); } if (null !== $documentAnnot->nodeType) { $metadata->setNodeType($documentAnnot->nodeType); } if (null !== $documentAnnot->repositoryClass) { $metadata->setCustomRepositoryClassName($documentAnnot->repositoryClass); } if (null !== $documentAnnot->translator) { $metadata->setTranslator($documentAnnot->translator); } foreach ($reflClass->getProperties() as $property) { if ($metadata->isInheritedField($property->name) && $metadata->name !== $property->getDeclaringClass()->getName()) { continue; } $mapping = array(); $mapping['fieldName'] = $property->getName(); foreach ($this->reader->getPropertyAnnotations($property) as $fieldAnnot) { if ($fieldAnnot instanceof ODM\Property) { $mapping = array_merge($mapping, (array) $fieldAnnot); $metadata->mapField($mapping); } elseif ($fieldAnnot instanceof ODM\Id) { $mapping = array_merge($mapping, (array) $fieldAnnot); $metadata->mapId($mapping); } elseif ($fieldAnnot instanceof ODM\Node) { $mapping = array_merge($mapping, (array) $fieldAnnot); $metadata->mapNode($mapping); } elseif ($fieldAnnot instanceof ODM\Nodename) { $mapping = array_merge($mapping, (array) $fieldAnnot); $metadata->mapNodename($mapping); } elseif ($fieldAnnot instanceof ODM\ParentDocument) { $mapping = array_merge($mapping, (array) $fieldAnnot); $mapping['cascade'] = $this->getCascadeMode($fieldAnnot->cascade); $metadata->mapParentDocument($mapping); } elseif ($fieldAnnot instanceof ODM\Child) { $mapping = array_merge($mapping, (array) $fieldAnnot); $mapping['cascade'] = $this->getCascadeMode($fieldAnnot->cascade); $metadata->mapChild($mapping); } elseif ($fieldAnnot instanceof ODM\Children) { $mapping = array_merge($mapping, (array) $fieldAnnot); $mapping['cascade'] = $this->getCascadeMode($fieldAnnot->cascade); $metadata->mapChildren($mapping); } elseif ($fieldAnnot instanceof ODM\ReferenceOne) { $mapping = array_merge($mapping, (array) $fieldAnnot); $mapping['cascade'] = $this->getCascadeMode($fieldAnnot->cascade); $metadata->mapManyToOne($mapping); } elseif ($fieldAnnot instanceof ODM\ReferenceMany) { $mapping = array_merge($mapping, (array) $fieldAnnot); $mapping['cascade'] = $this->getCascadeMode($fieldAnnot->cascade); $metadata->mapManyToMany($mapping); } elseif ($fieldAnnot instanceof ODM\Referrers) { $mapping = array_merge($mapping, (array) $fieldAnnot); $mapping['cascade'] = $this->getCascadeMode($fieldAnnot->cascade); $metadata->mapReferrers($mapping); } elseif ($fieldAnnot instanceof ODM\MixedReferrers) { $mapping = array_merge($mapping, (array) $fieldAnnot); $metadata->mapMixedReferrers($mapping); } elseif ($fieldAnnot instanceof ODM\Locale) { $mapping = array_merge($mapping, (array) $fieldAnnot); $metadata->mapLocale($mapping); } elseif ($fieldAnnot instanceof ODM\Depth) { $mapping = array_merge($mapping, (array) $fieldAnnot); $metadata->mapDepth($mapping); } elseif ($fieldAnnot instanceof ODM\VersionName) { $mapping = array_merge($mapping, (array) $fieldAnnot); $metadata->mapVersionName($mapping); } elseif ($fieldAnnot instanceof ODM\VersionCreated) { $mapping = array_merge($mapping, (array) $fieldAnnot); $metadata->mapVersionCreated($mapping); } } } foreach ($reflClass->getMethods() as $method) { if ($method->isPublic() && $method->getDeclaringClass()->getName() == $metadata->name) { foreach ($this->reader->getMethodAnnotations($method) as $annot) { if ($annot instanceof ODM\PrePersist) { $metadata->addLifecycleCallback($method->getName(), Event::prePersist); } elseif ($annot instanceof ODM\PostPersist) { $metadata->addLifecycleCallback($method->getName(), Event::postPersist); } elseif ($annot instanceof ODM\PreUpdate) { $metadata->addLifecycleCallback($method->getName(), Event::preUpdate); } elseif ($annot instanceof ODM\PostUpdate) { $metadata->addLifecycleCallback($method->getName(), Event::postUpdate); } elseif ($annot instanceof ODM\PreRemove) { $metadata->addLifecycleCallback($method->getName(), Event::preRemove); } elseif ($annot instanceof ODM\PostRemove) { $metadata->addLifecycleCallback($method->getName(), Event::postRemove); } elseif ($annot instanceof ODM\PostLoad) { $metadata->addLifecycleCallback($method->getName(), Event::postLoad); } } } } $metadata->validateClassMapping(); }
/** * {@inheritDoc} */ public function readExtendedMetadata(ClassMetadata $meta, array &$config) { $class = $meta->getReflectionClass(); // class annotations if ($annot = $this->reader->getClassAnnotation($class, self::TREE)) { if (!in_array($annot->type, $this->strategies)) { throw new InvalidMappingException("Tree type: {$annot->type} is not available."); } $config['strategy'] = $annot->type; } if ($annot = $this->reader->getClassAnnotation($class, self::CLOSURE)) { if (!class_exists($annot->class)) { throw new InvalidMappingException("Tree closure class: {$annot->class} does not exist."); } $config['closure'] = $annot->class; } // property annotations foreach ($class->getProperties() as $property) { if ($meta->isMappedSuperclass && !$property->isPrivate() || $meta->isInheritedField($property->name) || isset($meta->associationMappings[$property->name]['inherited'])) { continue; } // left if ($left = $this->reader->getPropertyAnnotation($property, self::LEFT)) { $field = $property->getName(); if (!$meta->hasField($field)) { throw new InvalidMappingException("Unable to find 'left' - [{$field}] as mapped property in entity - {$meta->name}"); } if (!$this->isValidField($meta, $field)) { throw new InvalidMappingException("Tree left field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}"); } $config['left'] = $field; } // right if ($right = $this->reader->getPropertyAnnotation($property, self::RIGHT)) { $field = $property->getName(); if (!$meta->hasField($field)) { throw new InvalidMappingException("Unable to find 'right' - [{$field}] as mapped property in entity - {$meta->name}"); } if (!$this->isValidField($meta, $field)) { throw new InvalidMappingException("Tree right field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}"); } $config['right'] = $field; } // ancestor/parent if ($parent = $this->reader->getPropertyAnnotation($property, self::PARENT)) { $field = $property->getName(); if (!$meta->isSingleValuedAssociation($field)) { throw new InvalidMappingException("Unable to find ancestor/parent child relation through ancestor field - [{$field}] in class - {$meta->name}"); } $config['parent'] = $field; } // root if ($root = $this->reader->getPropertyAnnotation($property, self::ROOT)) { $field = $property->getName(); if (!$meta->hasField($field)) { throw new InvalidMappingException("Unable to find 'root' - [{$field}] as mapped property in entity - {$meta->name}"); } if (!$this->isValidField($meta, $field)) { throw new InvalidMappingException("Tree root field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}"); } $config['root'] = $field; } // level if ($parent = $this->reader->getPropertyAnnotation($property, self::LEVEL)) { $field = $property->getName(); if (!$meta->hasField($field)) { throw new InvalidMappingException("Unable to find 'level' - [{$field}] as mapped property in entity - {$meta->name}"); } if (!$this->isValidField($meta, $field)) { throw new InvalidMappingException("Tree level field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}"); } $config['level'] = $field; } } }
/** * {@inheritdoc} */ public function loadMetadataForClass($className, ClassMetadata $metadata) { $reflClass = $metadata->getReflectionClass(); $documentAnnots = array(); foreach ($this->reader->getClassAnnotations($reflClass) as $annot) { foreach ($this->entityAnnotationClasses as $annotClass => $i) { if ($annot instanceof $annotClass) { $documentAnnots[$i] = $annot; } } } if (!$documentAnnots) { throw MappingException::classIsNotAValidDocument($className); } // find the winning document annotation ksort($documentAnnots); $documentAnnot = reset($documentAnnots); if (isset($documentAnnot->versionable) && $documentAnnot->versionable) { $metadata->setVersioned($documentAnnot->versionable); } $metadata->setNodeType($documentAnnot->nodeType); if (isset($documentAnnot->referenceable) && $documentAnnot->referenceable) { $metadata->setReferenceable(true); } if ($documentAnnot->repositoryClass) { $metadata->setCustomRepositoryClassName($documentAnnot->repositoryClass); } if ($documentAnnot->translator) { $metadata->setTranslator($documentAnnot->translator); } foreach ($reflClass->getProperties() as $property) { if ($metadata->isMappedSuperclass && !$property->isPrivate() || $metadata->isInheritedField($property->name) && $metadata->name !== $property->getDeclaringClass()->getName()) { continue; } $mapping = array(); $mapping['fieldName'] = $property->getName(); foreach ($this->reader->getPropertyAnnotations($property) as $fieldAnnot) { if ($fieldAnnot instanceof ODM\Property) { $mapping = array_merge($mapping, (array) $fieldAnnot); $metadata->mapField($mapping); } elseif ($fieldAnnot instanceof ODM\Id) { $mapping = array_merge($mapping, (array) $fieldAnnot); $metadata->mapId($mapping); } elseif ($fieldAnnot instanceof ODM\Node) { $mapping = array_merge($mapping, (array) $fieldAnnot); $metadata->mapNode($mapping); } elseif ($fieldAnnot instanceof ODM\Nodename) { $mapping = array_merge($mapping, (array) $fieldAnnot); $metadata->mapNodename($mapping); } elseif ($fieldAnnot instanceof ODM\ParentDocument) { $mapping = array_merge($mapping, (array) $fieldAnnot); $metadata->mapParentDocument($mapping); } elseif ($fieldAnnot instanceof ODM\Child) { $mapping = array_merge($mapping, (array) $fieldAnnot); $metadata->mapChild($mapping); } elseif ($fieldAnnot instanceof ODM\Children) { $mapping = array_merge($mapping, (array) $fieldAnnot); $metadata->mapChildren($mapping); } elseif ($fieldAnnot instanceof ODM\ReferenceOne) { $mapping = array_merge($mapping, (array) $fieldAnnot); $metadata->mapManyToOne($mapping); } elseif ($fieldAnnot instanceof ODM\ReferenceMany) { $mapping = array_merge($mapping, (array) $fieldAnnot); $metadata->mapManyToMany($mapping); } elseif ($fieldAnnot instanceof ODM\Referrers) { $mapping = array_merge($mapping, (array) $fieldAnnot); $metadata->mapReferrers($mapping); } elseif ($fieldAnnot instanceof ODM\Locale) { $mapping = array_merge($mapping, (array) $fieldAnnot); $metadata->mapLocale($mapping); } elseif ($fieldAnnot instanceof ODM\VersionName) { $mapping = array_merge($mapping, (array) $fieldAnnot); $metadata->mapVersionName($mapping); } elseif ($fieldAnnot instanceof ODM\VersionCreated) { $mapping = array_merge($mapping, (array) $fieldAnnot); $metadata->mapVersionCreated($mapping); } if (!isset($mapping['name'])) { $mapping['name'] = $property->getName(); } } } foreach ($reflClass->getMethods() as $method) { if ($method->isPublic() && $method->getDeclaringClass()->getName() == $metadata->name) { foreach ($this->reader->getMethodAnnotations($method) as $annot) { if ($annot instanceof ODM\PrePersist) { $metadata->addLifecycleCallback($method->getName(), Event::prePersist); } elseif ($annot instanceof ODM\PostPersist) { $metadata->addLifecycleCallback($method->getName(), Event::postPersist); } elseif ($annot instanceof ODM\PreUpdate) { $metadata->addLifecycleCallback($method->getName(), Event::preUpdate); } elseif ($annot instanceof ODM\PostUpdate) { $metadata->addLifecycleCallback($method->getName(), Event::postUpdate); } elseif ($annot instanceof ODM\PreRemove) { $metadata->addLifecycleCallback($method->getName(), Event::preRemove); } elseif ($annot instanceof ODM\PostRemove) { $metadata->addLifecycleCallback($method->getName(), Event::postRemove); } elseif ($annot instanceof ODM\PreLoad) { $metadata->addLifecycleCallback($method->getName(), Event::preLoad); } elseif ($annot instanceof ODM\PostLoad) { $metadata->addLifecycleCallback($method->getName(), Event::postLoad); } } } } // Check there is a @Locale annotation for translatable documents if (count($metadata->translatableFields)) { if (!isset($metadata->localeMapping)) { throw new MappingException("You must define a @Locale field for translatable document '{$className}'"); } } }
/** * {@inheritDoc} */ public function readExtendedMetadata(ClassMetadata $meta, array &$config) { $class = $meta->getReflectionClass(); // class annotations if ($annot = $this->reader->getClassAnnotation($class, self::HISTORYABLE)) { $config['historyable'] = true; } // property annotations foreach ($class->getProperties() as $property) { if ($meta->isMappedSuperclass && !$property->isPrivate() || $meta->isInheritedField($property->name) || isset($meta->associationMappings[$property->name]['inherited']) ) { continue; } if ($status = $this->reader->getPropertyAnnotation($property, self::STATUS)) { $field = $property->getName(); if (!$meta->hasField($field)) { throw new InvalidMappingException("Unable to find status [{$field}] as mapped property in entity - {$meta->name}"); } if (!$this->isValidField($meta, $field)) { throw new InvalidMappingException("Cannot make status on field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}"); } if (isset($config['status_field'])) { throw new InvalidMappingException("There cannot be more than one status field: [{$field}] and [{$config['status_field']}], in class - {$meta->name}."); } $config['status_field'] = $field; } if ($refVersion = $this->reader->getPropertyAnnotation($property, self::REFVERSION)) { $field = $property->getName(); if (isset($config['refversion_field'])) { throw new InvalidMappingException("There cannot be more than one refVersion field: [{$field}] and [{$config['status_refVersion']}], in class - {$meta->name}."); } $config['refVersion_field'] = $field; } } }
/** * {@inheritdoc} */ public function loadMetadataForClass($className, ClassMetadata $class) { $reflClass = $class->getReflectionClass(); $isValidDocument = false; $classAnnotations = $this->reader->getClassAnnotations($reflClass); foreach ($classAnnotations as $classAnnotation) { if ($classAnnotation instanceof ODM\Document) { if ($classAnnotation->indexed) { $class->indexed = true; } $class->setCustomRepositoryClass($classAnnotation->repositoryClass); $isValidDocument = true; } elseif ($classAnnotation instanceof ODM\EmbeddedDocument) { $class->isEmbeddedDocument = true; $isValidDocument = true; } else { if ($classAnnotation instanceof ODM\MappedSuperclass) { $class->isMappedSuperclass = true; $isValidDocument = true; } else { if ($classAnnotation instanceof ODM\Index) { $class->indexed = true; } else { if ($classAnnotation instanceof ODM\InheritanceRoot) { $class->markInheritanceRoot(); } } } } } if (!$isValidDocument) { throw MappingException::classIsNotAValidDocument($className); } foreach ($reflClass->getProperties() as $property) { if ($class->isInheritedAssociation($property->name) || $class->isInheritedField($property->name)) { continue; } $mapping = array(); $mapping['fieldName'] = $property->name; if ($this->reader->getPropertyAnnotation($property, 'Doctrine\\ODM\\CouchDB\\Mapping\\Annotations\\Index')) { $mapping['indexed'] = true; } foreach ($this->reader->getPropertyAnnotations($property) as $fieldAnnot) { if ($fieldAnnot instanceof \Doctrine\ODM\CouchDB\Mapping\Annotations\Field) { if ($fieldAnnot instanceof \Doctrine\ODM\CouchDB\Mapping\Annotations\Version) { $mapping['isVersionField'] = true; } $mapping = array_merge($mapping, (array) $fieldAnnot); unset($mapping['value']); $class->mapField($mapping); } else { if ($fieldAnnot instanceof \Doctrine\ODM\CouchDB\Mapping\Annotations\ReferenceOne) { $mapping = array_merge($mapping, (array) $fieldAnnot); $mapping['cascade'] = $this->getCascadeMode($fieldAnnot->cascade); unset($mapping['value']); $class->mapManyToOne($mapping); } else { if ($fieldAnnot instanceof \Doctrine\ODM\CouchDB\Mapping\Annotations\ReferenceMany) { $mapping = array_merge($mapping, (array) $fieldAnnot); $mapping['cascade'] = $this->getCascadeMode($fieldAnnot->cascade); unset($mapping['value']); $class->mapManyToMany($mapping); } else { if ($fieldAnnot instanceof \Doctrine\ODM\CouchDB\Mapping\Annotations\Attachments) { $class->mapAttachments($mapping['fieldName']); } else { if ($fieldAnnot instanceof \Doctrine\ODM\CouchDB\Mapping\Annotations\EmbedOne || $fieldAnnot instanceof \Doctrine\ODM\CouchDB\Mapping\Annotations\EmbedMany) { $mapping = array_merge($mapping, (array) $fieldAnnot); unset($mapping['value']); $class->mapEmbedded($mapping); } } } } } } } }
/** * @param Common\Persistence\Mapping\ClassMetadata $class * * @return array * * @throws ORM\Mapping\MappingException */ private function buildPhoneFields(Common\Persistence\Mapping\ClassMetadata $class) { $phoneFields = []; foreach ($class->getFieldNames() as $fieldName) { $mapping = $class->getFieldMapping($fieldName); if ($mapping['type'] !== Types\Phone::PHONE) { continue; } $classReflection = $class->isInheritedField($fieldName) ? new \ReflectionClass($mapping['declared']) : $class->getReflectionClass(); $phoneFields[$fieldName] = ['phoneFieldClass' => $classReflection->getName()]; } return $phoneFields; }