/** * {@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 loadMetadataForClass($className, ClassMetadata $metadata) { /* @var $metadata \Doctrine\ORM\Mapping\ClassMetadataInfo */ /* @var $xmlRoot SimpleXMLElement */ $xmlRoot = $this->getElement($className); if ($xmlRoot->getName() == 'entity') { if (isset($xmlRoot['repository-class'])) { $metadata->setCustomRepositoryClass((string) $xmlRoot['repository-class']); } if (isset($xmlRoot['read-only']) && $this->evaluateBoolean($xmlRoot['read-only'])) { $metadata->markReadOnly(); } } else { if ($xmlRoot->getName() == 'mapped-superclass') { $metadata->setCustomRepositoryClass(isset($xmlRoot['repository-class']) ? (string) $xmlRoot['repository-class'] : null); $metadata->isMappedSuperclass = true; } else { if ($xmlRoot->getName() == 'embeddable') { $metadata->isEmbeddedClass = true; } else { throw MappingException::classIsNotAValidEntityOrMappedSuperClass($className); } } } // Evaluate <entity...> attributes $primaryTable = array(); if (isset($xmlRoot['table'])) { $primaryTable['name'] = (string) $xmlRoot['table']; } if (isset($xmlRoot['schema'])) { $primaryTable['schema'] = (string) $xmlRoot['schema']; } $metadata->setPrimaryTable($primaryTable); // Evaluate second level cache if (isset($xmlRoot->cache)) { $metadata->enableCache($this->cacheToArray($xmlRoot->cache)); } // Evaluate named queries if (isset($xmlRoot->{'named-queries'})) { foreach ($xmlRoot->{'named-queries'}->{'named-query'} as $namedQueryElement) { $metadata->addNamedQuery(array('name' => (string) $namedQueryElement['name'], 'query' => (string) $namedQueryElement['query'])); } } // Evaluate native named queries if (isset($xmlRoot->{'named-native-queries'})) { foreach ($xmlRoot->{'named-native-queries'}->{'named-native-query'} as $nativeQueryElement) { $metadata->addNamedNativeQuery(array('name' => isset($nativeQueryElement['name']) ? (string) $nativeQueryElement['name'] : null, 'query' => isset($nativeQueryElement->query) ? (string) $nativeQueryElement->query : null, 'resultClass' => isset($nativeQueryElement['result-class']) ? (string) $nativeQueryElement['result-class'] : null, 'resultSetMapping' => isset($nativeQueryElement['result-set-mapping']) ? (string) $nativeQueryElement['result-set-mapping'] : null)); } } // Evaluate sql result set mapping if (isset($xmlRoot->{'sql-result-set-mappings'})) { foreach ($xmlRoot->{'sql-result-set-mappings'}->{'sql-result-set-mapping'} as $rsmElement) { $entities = array(); $columns = array(); foreach ($rsmElement as $entityElement) { //<entity-result/> if (isset($entityElement['entity-class'])) { $entityResult = array('fields' => array(), 'entityClass' => (string) $entityElement['entity-class'], 'discriminatorColumn' => isset($entityElement['discriminator-column']) ? (string) $entityElement['discriminator-column'] : null); foreach ($entityElement as $fieldElement) { $entityResult['fields'][] = array('name' => isset($fieldElement['name']) ? (string) $fieldElement['name'] : null, 'column' => isset($fieldElement['column']) ? (string) $fieldElement['column'] : null); } $entities[] = $entityResult; } //<column-result/> if (isset($entityElement['name'])) { $columns[] = array('name' => (string) $entityElement['name']); } } $metadata->addSqlResultSetMapping(array('name' => (string) $rsmElement['name'], 'entities' => $entities, 'columns' => $columns)); } } if (isset($xmlRoot['inheritance-type'])) { $inheritanceType = (string) $xmlRoot['inheritance-type']; $metadata->setInheritanceType(constant('Doctrine\\ORM\\Mapping\\ClassMetadata::INHERITANCE_TYPE_' . $inheritanceType)); if ($metadata->inheritanceType != \Doctrine\ORM\Mapping\ClassMetadata::INHERITANCE_TYPE_NONE) { // Evaluate <discriminator-column...> if (isset($xmlRoot->{'discriminator-column'})) { $discrColumn = $xmlRoot->{'discriminator-column'}; $metadata->setDiscriminatorColumn(array('name' => isset($discrColumn['name']) ? (string) $discrColumn['name'] : null, 'type' => isset($discrColumn['type']) ? (string) $discrColumn['type'] : null, 'length' => isset($discrColumn['length']) ? (string) $discrColumn['length'] : null, 'columnDefinition' => isset($discrColumn['column-definition']) ? (string) $discrColumn['column-definition'] : null)); } else { $metadata->setDiscriminatorColumn(array('name' => 'dtype', 'type' => 'string', 'length' => 255)); } // Evaluate <discriminator-map...> if (isset($xmlRoot->{'discriminator-map'})) { $map = array(); foreach ($xmlRoot->{'discriminator-map'}->{'discriminator-mapping'} as $discrMapElement) { $map[(string) $discrMapElement['value']] = (string) $discrMapElement['class']; } $metadata->setDiscriminatorMap($map); } } } // Evaluate <change-tracking-policy...> if (isset($xmlRoot['change-tracking-policy'])) { $metadata->setChangeTrackingPolicy(constant('Doctrine\\ORM\\Mapping\\ClassMetadata::CHANGETRACKING_' . strtoupper((string) $xmlRoot['change-tracking-policy']))); } // Evaluate <indexes...> if (isset($xmlRoot->indexes)) { $metadata->table['indexes'] = array(); foreach ($xmlRoot->indexes->index as $indexXml) { $index = array('columns' => explode(',', (string) $indexXml['columns'])); if (isset($indexXml['flags'])) { $index['flags'] = explode(',', (string) $indexXml['flags']); } if (isset($indexXml->options)) { $index['options'] = $this->_parseOptions($indexXml->options->children()); } if (isset($indexXml['name'])) { $metadata->table['indexes'][(string) $indexXml['name']] = $index; } else { $metadata->table['indexes'][] = $index; } } } // Evaluate <unique-constraints..> if (isset($xmlRoot->{'unique-constraints'})) { $metadata->table['uniqueConstraints'] = array(); foreach ($xmlRoot->{'unique-constraints'}->{'unique-constraint'} as $uniqueXml) { $unique = array('columns' => explode(',', (string) $uniqueXml['columns'])); if (isset($uniqueXml->options)) { $unique['options'] = $this->_parseOptions($uniqueXml->options->children()); } if (isset($uniqueXml['name'])) { $metadata->table['uniqueConstraints'][(string) $uniqueXml['name']] = $unique; } else { $metadata->table['uniqueConstraints'][] = $unique; } } } if (isset($xmlRoot->options)) { $metadata->table['options'] = $this->_parseOptions($xmlRoot->options->children()); } // The mapping assignment is done in 2 times as a bug might occurs on some php/xml lib versions // The internal SimpleXmlIterator get resetted, to this generate a duplicate field exception $mappings = array(); // Evaluate <field ...> mappings if (isset($xmlRoot->field)) { foreach ($xmlRoot->field as $fieldMapping) { $mapping = $this->columnToArray($fieldMapping); if (isset($mapping['version'])) { $metadata->setVersionMapping($mapping); unset($mapping['version']); } $metadata->mapField($mapping); } } if (isset($xmlRoot->embedded)) { foreach ($xmlRoot->embedded as $embeddedMapping) { $columnPrefix = isset($embeddedMapping['column-prefix']) ? (string) $embeddedMapping['column-prefix'] : null; $useColumnPrefix = isset($embeddedMapping['use-column-prefix']) ? $this->evaluateBoolean($embeddedMapping['use-column-prefix']) : true; $mapping = array('fieldName' => (string) $embeddedMapping['name'], 'class' => (string) $embeddedMapping['class'], 'columnPrefix' => $useColumnPrefix ? $columnPrefix : false); $metadata->mapEmbedded($mapping); } } foreach ($mappings as $mapping) { if (isset($mapping['version'])) { $metadata->setVersionMapping($mapping); } $metadata->mapField($mapping); } // Evaluate <id ...> mappings $associationIds = array(); foreach ($xmlRoot->id as $idElement) { if (isset($idElement['association-key']) && $this->evaluateBoolean($idElement['association-key'])) { $associationIds[(string) $idElement['name']] = true; continue; } $mapping = array('id' => true, 'fieldName' => (string) $idElement['name']); if (isset($idElement['type'])) { $mapping['type'] = (string) $idElement['type']; } if (isset($idElement['length'])) { $mapping['length'] = (string) $idElement['length']; } if (isset($idElement['column'])) { $mapping['columnName'] = (string) $idElement['column']; } if (isset($idElement['column-definition'])) { $mapping['columnDefinition'] = (string) $idElement['column-definition']; } if (isset($idElement->options)) { $mapping['options'] = $this->_parseOptions($idElement->options->children()); } $metadata->mapField($mapping); if (isset($idElement->generator)) { $strategy = isset($idElement->generator['strategy']) ? (string) $idElement->generator['strategy'] : 'AUTO'; $metadata->setIdGeneratorType(constant('Doctrine\\ORM\\Mapping\\ClassMetadata::GENERATOR_TYPE_' . $strategy)); } // Check for SequenceGenerator/TableGenerator definition if (isset($idElement->{'sequence-generator'})) { $seqGenerator = $idElement->{'sequence-generator'}; $metadata->setSequenceGeneratorDefinition(array('sequenceName' => (string) $seqGenerator['sequence-name'], 'allocationSize' => (string) $seqGenerator['allocation-size'], 'initialValue' => (string) $seqGenerator['initial-value'])); } else { if (isset($idElement->{'custom-id-generator'})) { $customGenerator = $idElement->{'custom-id-generator'}; $metadata->setCustomGeneratorDefinition(array('class' => (string) $customGenerator['class'])); } else { if (isset($idElement->{'table-generator'})) { throw MappingException::tableIdGeneratorNotImplemented($className); } } } } // Evaluate <one-to-one ...> mappings if (isset($xmlRoot->{'one-to-one'})) { foreach ($xmlRoot->{'one-to-one'} as $oneToOneElement) { $mapping = array('fieldName' => (string) $oneToOneElement['field'], 'targetEntity' => (string) $oneToOneElement['target-entity']); if (isset($associationIds[$mapping['fieldName']])) { $mapping['id'] = true; } if (isset($oneToOneElement['fetch'])) { $mapping['fetch'] = constant('Doctrine\\ORM\\Mapping\\ClassMetadata::FETCH_' . (string) $oneToOneElement['fetch']); } if (isset($oneToOneElement['mapped-by'])) { $mapping['mappedBy'] = (string) $oneToOneElement['mapped-by']; } else { if (isset($oneToOneElement['inversed-by'])) { $mapping['inversedBy'] = (string) $oneToOneElement['inversed-by']; } $joinColumns = array(); if (isset($oneToOneElement->{'join-column'})) { $joinColumns[] = $this->joinColumnToArray($oneToOneElement->{'join-column'}); } else { if (isset($oneToOneElement->{'join-columns'})) { foreach ($oneToOneElement->{'join-columns'}->{'join-column'} as $joinColumnElement) { $joinColumns[] = $this->joinColumnToArray($joinColumnElement); } } } $mapping['joinColumns'] = $joinColumns; } if (isset($oneToOneElement->cascade)) { $mapping['cascade'] = $this->_getCascadeMappings($oneToOneElement->cascade); } if (isset($oneToOneElement['orphan-removal'])) { $mapping['orphanRemoval'] = $this->evaluateBoolean($oneToOneElement['orphan-removal']); } $metadata->mapOneToOne($mapping); // Evaluate second level cache if (isset($oneToOneElement->cache)) { $metadata->enableAssociationCache($mapping['fieldName'], $this->cacheToArray($oneToOneElement->cache)); } } } // Evaluate <one-to-many ...> mappings if (isset($xmlRoot->{'one-to-many'})) { foreach ($xmlRoot->{'one-to-many'} as $oneToManyElement) { $mapping = array('fieldName' => (string) $oneToManyElement['field'], 'targetEntity' => (string) $oneToManyElement['target-entity'], 'mappedBy' => (string) $oneToManyElement['mapped-by']); if (isset($oneToManyElement['fetch'])) { $mapping['fetch'] = constant('Doctrine\\ORM\\Mapping\\ClassMetadata::FETCH_' . (string) $oneToManyElement['fetch']); } if (isset($oneToManyElement->cascade)) { $mapping['cascade'] = $this->_getCascadeMappings($oneToManyElement->cascade); } if (isset($oneToManyElement['orphan-removal'])) { $mapping['orphanRemoval'] = $this->evaluateBoolean($oneToManyElement['orphan-removal']); } if (isset($oneToManyElement->{'order-by'})) { $orderBy = array(); foreach ($oneToManyElement->{'order-by'}->{'order-by-field'} as $orderByField) { $orderBy[(string) $orderByField['name']] = (string) $orderByField['direction']; } $mapping['orderBy'] = $orderBy; } if (isset($oneToManyElement['index-by'])) { $mapping['indexBy'] = (string) $oneToManyElement['index-by']; } else { if (isset($oneToManyElement->{'index-by'})) { throw new \InvalidArgumentException("<index-by /> is not a valid tag"); } } $metadata->mapOneToMany($mapping); // Evaluate second level cache if (isset($oneToManyElement->cache)) { $metadata->enableAssociationCache($mapping['fieldName'], $this->cacheToArray($oneToManyElement->cache)); } } } // Evaluate <many-to-one ...> mappings if (isset($xmlRoot->{'many-to-one'})) { foreach ($xmlRoot->{'many-to-one'} as $manyToOneElement) { $mapping = array('fieldName' => (string) $manyToOneElement['field'], 'targetEntity' => (string) $manyToOneElement['target-entity']); if (isset($associationIds[$mapping['fieldName']])) { $mapping['id'] = true; } if (isset($manyToOneElement['fetch'])) { $mapping['fetch'] = constant('Doctrine\\ORM\\Mapping\\ClassMetadata::FETCH_' . (string) $manyToOneElement['fetch']); } if (isset($manyToOneElement['inversed-by'])) { $mapping['inversedBy'] = (string) $manyToOneElement['inversed-by']; } $joinColumns = array(); if (isset($manyToOneElement->{'join-column'})) { $joinColumns[] = $this->joinColumnToArray($manyToOneElement->{'join-column'}); } else { if (isset($manyToOneElement->{'join-columns'})) { foreach ($manyToOneElement->{'join-columns'}->{'join-column'} as $joinColumnElement) { $joinColumns[] = $this->joinColumnToArray($joinColumnElement); } } } $mapping['joinColumns'] = $joinColumns; if (isset($manyToOneElement->cascade)) { $mapping['cascade'] = $this->_getCascadeMappings($manyToOneElement->cascade); } $metadata->mapManyToOne($mapping); // Evaluate second level cache if (isset($manyToOneElement->cache)) { $metadata->enableAssociationCache($mapping['fieldName'], $this->cacheToArray($manyToOneElement->cache)); } } } // Evaluate <many-to-many ...> mappings if (isset($xmlRoot->{'many-to-many'})) { foreach ($xmlRoot->{'many-to-many'} as $manyToManyElement) { $mapping = array('fieldName' => (string) $manyToManyElement['field'], 'targetEntity' => (string) $manyToManyElement['target-entity']); if (isset($manyToManyElement['fetch'])) { $mapping['fetch'] = constant('Doctrine\\ORM\\Mapping\\ClassMetadata::FETCH_' . (string) $manyToManyElement['fetch']); } if (isset($manyToManyElement['orphan-removal'])) { $mapping['orphanRemoval'] = $this->evaluateBoolean($manyToManyElement['orphan-removal']); } if (isset($manyToManyElement['mapped-by'])) { $mapping['mappedBy'] = (string) $manyToManyElement['mapped-by']; } else { if (isset($manyToManyElement->{'join-table'})) { if (isset($manyToManyElement['inversed-by'])) { $mapping['inversedBy'] = (string) $manyToManyElement['inversed-by']; } $joinTableElement = $manyToManyElement->{'join-table'}; $joinTable = array('name' => (string) $joinTableElement['name']); if (isset($joinTableElement['schema'])) { $joinTable['schema'] = (string) $joinTableElement['schema']; } foreach ($joinTableElement->{'join-columns'}->{'join-column'} as $joinColumnElement) { $joinTable['joinColumns'][] = $this->joinColumnToArray($joinColumnElement); } foreach ($joinTableElement->{'inverse-join-columns'}->{'join-column'} as $joinColumnElement) { $joinTable['inverseJoinColumns'][] = $this->joinColumnToArray($joinColumnElement); } $mapping['joinTable'] = $joinTable; } } if (isset($manyToManyElement->cascade)) { $mapping['cascade'] = $this->_getCascadeMappings($manyToManyElement->cascade); } if (isset($manyToManyElement->{'order-by'})) { $orderBy = array(); foreach ($manyToManyElement->{'order-by'}->{'order-by-field'} as $orderByField) { $orderBy[(string) $orderByField['name']] = (string) $orderByField['direction']; } $mapping['orderBy'] = $orderBy; } if (isset($manyToManyElement['index-by'])) { $mapping['indexBy'] = (string) $manyToManyElement['index-by']; } else { if (isset($manyToManyElement->{'index-by'})) { throw new \InvalidArgumentException("<index-by /> is not a valid tag"); } } $metadata->mapManyToMany($mapping); // Evaluate second level cache if (isset($manyToManyElement->cache)) { $metadata->enableAssociationCache($mapping['fieldName'], $this->cacheToArray($manyToManyElement->cache)); } } } // Evaluate association-overrides if (isset($xmlRoot->{'attribute-overrides'})) { foreach ($xmlRoot->{'attribute-overrides'}->{'attribute-override'} as $overrideElement) { $fieldName = (string) $overrideElement['name']; foreach ($overrideElement->field as $field) { $mapping = $this->columnToArray($field); $mapping['fieldName'] = $fieldName; $metadata->setAttributeOverride($fieldName, $mapping); } } } // Evaluate association-overrides if (isset($xmlRoot->{'association-overrides'})) { foreach ($xmlRoot->{'association-overrides'}->{'association-override'} as $overrideElement) { $fieldName = (string) $overrideElement['name']; $override = array(); // Check for join-columns if (isset($overrideElement->{'join-columns'})) { $joinColumns = array(); foreach ($overrideElement->{'join-columns'}->{'join-column'} as $joinColumnElement) { $joinColumns[] = $this->joinColumnToArray($joinColumnElement); } $override['joinColumns'] = $joinColumns; } // Check for join-table if ($overrideElement->{'join-table'}) { $joinTable = null; $joinTableElement = $overrideElement->{'join-table'}; $joinTable = array('name' => (string) $joinTableElement['name'], 'schema' => (string) $joinTableElement['schema']); if (isset($joinTableElement->{'join-columns'})) { foreach ($joinTableElement->{'join-columns'}->{'join-column'} as $joinColumnElement) { $joinTable['joinColumns'][] = $this->joinColumnToArray($joinColumnElement); } } if (isset($joinTableElement->{'inverse-join-columns'})) { foreach ($joinTableElement->{'inverse-join-columns'}->{'join-column'} as $joinColumnElement) { $joinTable['inverseJoinColumns'][] = $this->joinColumnToArray($joinColumnElement); } } $override['joinTable'] = $joinTable; } $metadata->setAssociationOverride($fieldName, $override); } } // Evaluate <lifecycle-callbacks...> if (isset($xmlRoot->{'lifecycle-callbacks'})) { foreach ($xmlRoot->{'lifecycle-callbacks'}->{'lifecycle-callback'} as $lifecycleCallback) { $metadata->addLifecycleCallback((string) $lifecycleCallback['method'], constant('Doctrine\\ORM\\Events::' . (string) $lifecycleCallback['type'])); } } // Evaluate entity listener if (isset($xmlRoot->{'entity-listeners'})) { foreach ($xmlRoot->{'entity-listeners'}->{'entity-listener'} as $listenerElement) { $className = (string) $listenerElement['class']; // Evaluate the listener using naming convention. if ($listenerElement->count() === 0) { EntityListenerBuilder::bindEntityListener($metadata, $className); continue; } foreach ($listenerElement as $callbackElement) { $eventName = (string) $callbackElement['type']; $methodName = (string) $callbackElement['method']; $metadata->addEntityListener($eventName, $className, $methodName); } } } }
/** * {@inheritDoc} */ public function loadMetadataForClass($className, ClassMetadata $metadata) { /* @var $metadata \Doctrine\ORM\Mapping\ClassMetadataInfo */ $element = $this->getElement($className); if ($element['type'] == 'entity') { if (isset($element['repositoryClass'])) { $metadata->setCustomRepositoryClass($element['repositoryClass']); } if (isset($element['readOnly']) && $element['readOnly'] == true) { $metadata->markReadOnly(); } } else { if ($element['type'] == 'mappedSuperclass') { $metadata->setCustomRepositoryClass(isset($element['repositoryClass']) ? $element['repositoryClass'] : null); $metadata->isMappedSuperclass = true; } else { if ($element['type'] == 'embeddable') { $metadata->isEmbeddedClass = true; } else { throw MappingException::classIsNotAValidEntityOrMappedSuperClass($className); } } } // Evaluate root level properties $table = array(); if (isset($element['table'])) { $table['name'] = $element['table']; } // Evaluate second level cache if (isset($element['cache'])) { $metadata->enableCache($this->cacheToArray($element['cache'])); } $metadata->setPrimaryTable($table); // Evaluate named queries if (isset($element['namedQueries'])) { foreach ($element['namedQueries'] as $name => $queryMapping) { if (is_string($queryMapping)) { $queryMapping = array('query' => $queryMapping); } if (!isset($queryMapping['name'])) { $queryMapping['name'] = $name; } $metadata->addNamedQuery($queryMapping); } } // Evaluate named native queries if (isset($element['namedNativeQueries'])) { foreach ($element['namedNativeQueries'] as $name => $mappingElement) { if (!isset($mappingElement['name'])) { $mappingElement['name'] = $name; } $metadata->addNamedNativeQuery(array('name' => $mappingElement['name'], 'query' => isset($mappingElement['query']) ? $mappingElement['query'] : null, 'resultClass' => isset($mappingElement['resultClass']) ? $mappingElement['resultClass'] : null, 'resultSetMapping' => isset($mappingElement['resultSetMapping']) ? $mappingElement['resultSetMapping'] : null)); } } // Evaluate sql result set mappings if (isset($element['sqlResultSetMappings'])) { foreach ($element['sqlResultSetMappings'] as $name => $resultSetMapping) { if (!isset($resultSetMapping['name'])) { $resultSetMapping['name'] = $name; } $entities = array(); $columns = array(); if (isset($resultSetMapping['entityResult'])) { foreach ($resultSetMapping['entityResult'] as $entityResultElement) { $entityResult = array('fields' => array(), 'entityClass' => isset($entityResultElement['entityClass']) ? $entityResultElement['entityClass'] : null, 'discriminatorColumn' => isset($entityResultElement['discriminatorColumn']) ? $entityResultElement['discriminatorColumn'] : null); if (isset($entityResultElement['fieldResult'])) { foreach ($entityResultElement['fieldResult'] as $fieldResultElement) { $entityResult['fields'][] = array('name' => isset($fieldResultElement['name']) ? $fieldResultElement['name'] : null, 'column' => isset($fieldResultElement['column']) ? $fieldResultElement['column'] : null); } } $entities[] = $entityResult; } } if (isset($resultSetMapping['columnResult'])) { foreach ($resultSetMapping['columnResult'] as $columnResultAnnot) { $columns[] = array('name' => isset($columnResultAnnot['name']) ? $columnResultAnnot['name'] : null); } } $metadata->addSqlResultSetMapping(array('name' => $resultSetMapping['name'], 'entities' => $entities, 'columns' => $columns)); } } /* not implemented specially anyway. use table = schema.table if (isset($element['schema'])) { $metadata->table['schema'] = $element['schema']; }*/ if (isset($element['inheritanceType'])) { $metadata->setInheritanceType(constant('Doctrine\\ORM\\Mapping\\ClassMetadata::INHERITANCE_TYPE_' . strtoupper($element['inheritanceType']))); if ($metadata->inheritanceType != \Doctrine\ORM\Mapping\ClassMetadata::INHERITANCE_TYPE_NONE) { // Evaluate discriminatorColumn if (isset($element['discriminatorColumn'])) { $discrColumn = $element['discriminatorColumn']; $metadata->setDiscriminatorColumn(array('name' => isset($discrColumn['name']) ? (string) $discrColumn['name'] : null, 'type' => isset($discrColumn['type']) ? (string) $discrColumn['type'] : null, 'length' => isset($discrColumn['length']) ? (string) $discrColumn['length'] : null, 'columnDefinition' => isset($discrColumn['columnDefinition']) ? (string) $discrColumn['columnDefinition'] : null)); } else { $metadata->setDiscriminatorColumn(array('name' => 'dtype', 'type' => 'string', 'length' => 255)); } // Evaluate discriminatorMap if (isset($element['discriminatorMap'])) { $metadata->setDiscriminatorMap($element['discriminatorMap']); } } } // Evaluate changeTrackingPolicy if (isset($element['changeTrackingPolicy'])) { $metadata->setChangeTrackingPolicy(constant('Doctrine\\ORM\\Mapping\\ClassMetadata::CHANGETRACKING_' . strtoupper($element['changeTrackingPolicy']))); } // Evaluate indexes if (isset($element['indexes'])) { foreach ($element['indexes'] as $name => $indexYml) { if (!isset($indexYml['name'])) { $indexYml['name'] = $name; } if (is_string($indexYml['columns'])) { $index = array('columns' => array_map('trim', explode(',', $indexYml['columns']))); } else { $index = array('columns' => $indexYml['columns']); } if (isset($indexYml['flags'])) { if (is_string($indexYml['flags'])) { $index['flags'] = array_map('trim', explode(',', $indexYml['flags'])); } else { $index['flags'] = $indexYml['flags']; } } if (isset($indexYml['options'])) { $index['options'] = $indexYml['options']; } $metadata->table['indexes'][$indexYml['name']] = $index; } } // Evaluate uniqueConstraints if (isset($element['uniqueConstraints'])) { foreach ($element['uniqueConstraints'] as $name => $uniqueYml) { if (!isset($uniqueYml['name'])) { $uniqueYml['name'] = $name; } if (is_string($uniqueYml['columns'])) { $unique = array('columns' => array_map('trim', explode(',', $uniqueYml['columns']))); } else { $unique = array('columns' => $uniqueYml['columns']); } if (isset($uniqueYml['options'])) { $unique['options'] = $uniqueYml['options']; } $metadata->table['uniqueConstraints'][$uniqueYml['name']] = $unique; } } if (isset($element['options'])) { $metadata->table['options'] = $element['options']; } $associationIds = array(); if (isset($element['id'])) { // Evaluate identifier settings foreach ($element['id'] as $name => $idElement) { if (isset($idElement['associationKey']) && $idElement['associationKey'] == true) { $associationIds[$name] = true; continue; } $mapping = array('id' => true, 'fieldName' => $name); if (isset($idElement['type'])) { $mapping['type'] = $idElement['type']; } if (isset($idElement['column'])) { $mapping['columnName'] = $idElement['column']; } if (isset($idElement['length'])) { $mapping['length'] = $idElement['length']; } if (isset($idElement['columnDefinition'])) { $mapping['columnDefinition'] = $idElement['columnDefinition']; } if (isset($idElement['options'])) { $mapping['options'] = $idElement['options']; } $metadata->mapField($mapping); if (isset($idElement['generator'])) { $metadata->setIdGeneratorType(constant('Doctrine\\ORM\\Mapping\\ClassMetadata::GENERATOR_TYPE_' . strtoupper($idElement['generator']['strategy']))); } // Check for SequenceGenerator/TableGenerator definition if (isset($idElement['sequenceGenerator'])) { $metadata->setSequenceGeneratorDefinition($idElement['sequenceGenerator']); } else { if (isset($idElement['customIdGenerator'])) { $customGenerator = $idElement['customIdGenerator']; $metadata->setCustomGeneratorDefinition(array('class' => (string) $customGenerator['class'])); } else { if (isset($idElement['tableGenerator'])) { throw MappingException::tableIdGeneratorNotImplemented($className); } } } } } // Evaluate fields if (isset($element['fields'])) { foreach ($element['fields'] as $name => $fieldMapping) { $mapping = $this->columnToArray($name, $fieldMapping); if (isset($fieldMapping['id'])) { $mapping['id'] = true; if (isset($fieldMapping['generator']['strategy'])) { $metadata->setIdGeneratorType(constant('Doctrine\\ORM\\Mapping\\ClassMetadata::GENERATOR_TYPE_' . strtoupper($fieldMapping['generator']['strategy']))); } } if (isset($mapping['version'])) { $metadata->setVersionMapping($mapping); unset($mapping['version']); } $metadata->mapField($mapping); } } if (isset($element['embedded'])) { foreach ($element['embedded'] as $name => $embeddedMapping) { $mapping = array('fieldName' => $name, 'class' => $embeddedMapping['class'], 'columnPrefix' => isset($embeddedMapping['columnPrefix']) ? $embeddedMapping['columnPrefix'] : null); $metadata->mapEmbedded($mapping); } } // Evaluate oneToOne relationships if (isset($element['oneToOne'])) { foreach ($element['oneToOne'] as $name => $oneToOneElement) { $mapping = array('fieldName' => $name, 'targetEntity' => $oneToOneElement['targetEntity']); if (isset($associationIds[$mapping['fieldName']])) { $mapping['id'] = true; } if (isset($oneToOneElement['fetch'])) { $mapping['fetch'] = constant('Doctrine\\ORM\\Mapping\\ClassMetadata::FETCH_' . $oneToOneElement['fetch']); } if (isset($oneToOneElement['mappedBy'])) { $mapping['mappedBy'] = $oneToOneElement['mappedBy']; } else { if (isset($oneToOneElement['inversedBy'])) { $mapping['inversedBy'] = $oneToOneElement['inversedBy']; } $joinColumns = array(); if (isset($oneToOneElement['joinColumn'])) { $joinColumns[] = $this->joinColumnToArray($oneToOneElement['joinColumn']); } else { if (isset($oneToOneElement['joinColumns'])) { foreach ($oneToOneElement['joinColumns'] as $joinColumnName => $joinColumnElement) { if (!isset($joinColumnElement['name'])) { $joinColumnElement['name'] = $joinColumnName; } $joinColumns[] = $this->joinColumnToArray($joinColumnElement); } } } $mapping['joinColumns'] = $joinColumns; } if (isset($oneToOneElement['cascade'])) { $mapping['cascade'] = $oneToOneElement['cascade']; } if (isset($oneToOneElement['orphanRemoval'])) { $mapping['orphanRemoval'] = (bool) $oneToOneElement['orphanRemoval']; } $metadata->mapOneToOne($mapping); // Evaluate second level cache if (isset($oneToOneElement['cache'])) { $metadata->enableAssociationCache($mapping['fieldName'], $this->cacheToArray($oneToOneElement['cache'])); } } } // Evaluate oneToMany relationships if (isset($element['oneToMany'])) { foreach ($element['oneToMany'] as $name => $oneToManyElement) { $mapping = array('fieldName' => $name, 'targetEntity' => $oneToManyElement['targetEntity'], 'mappedBy' => $oneToManyElement['mappedBy']); if (isset($oneToManyElement['fetch'])) { $mapping['fetch'] = constant('Doctrine\\ORM\\Mapping\\ClassMetadata::FETCH_' . $oneToManyElement['fetch']); } if (isset($oneToManyElement['cascade'])) { $mapping['cascade'] = $oneToManyElement['cascade']; } if (isset($oneToManyElement['orphanRemoval'])) { $mapping['orphanRemoval'] = (bool) $oneToManyElement['orphanRemoval']; } if (isset($oneToManyElement['orderBy'])) { $mapping['orderBy'] = $oneToManyElement['orderBy']; } if (isset($oneToManyElement['indexBy'])) { $mapping['indexBy'] = $oneToManyElement['indexBy']; } $metadata->mapOneToMany($mapping); // Evaluate second level cache if (isset($oneToManyElement['cache'])) { $metadata->enableAssociationCache($mapping['fieldName'], $this->cacheToArray($oneToManyElement['cache'])); } } } // Evaluate manyToOne relationships if (isset($element['manyToOne'])) { foreach ($element['manyToOne'] as $name => $manyToOneElement) { $mapping = array('fieldName' => $name, 'targetEntity' => $manyToOneElement['targetEntity']); if (isset($associationIds[$mapping['fieldName']])) { $mapping['id'] = true; } if (isset($manyToOneElement['fetch'])) { $mapping['fetch'] = constant('Doctrine\\ORM\\Mapping\\ClassMetadata::FETCH_' . $manyToOneElement['fetch']); } if (isset($manyToOneElement['inversedBy'])) { $mapping['inversedBy'] = $manyToOneElement['inversedBy']; } $joinColumns = array(); if (isset($manyToOneElement['joinColumn'])) { $joinColumns[] = $this->joinColumnToArray($manyToOneElement['joinColumn']); } else { if (isset($manyToOneElement['joinColumns'])) { foreach ($manyToOneElement['joinColumns'] as $joinColumnName => $joinColumnElement) { if (!isset($joinColumnElement['name'])) { $joinColumnElement['name'] = $joinColumnName; } $joinColumns[] = $this->joinColumnToArray($joinColumnElement); } } } $mapping['joinColumns'] = $joinColumns; if (isset($manyToOneElement['cascade'])) { $mapping['cascade'] = $manyToOneElement['cascade']; } $metadata->mapManyToOne($mapping); // Evaluate second level cache if (isset($manyToOneElement['cache'])) { $metadata->enableAssociationCache($mapping['fieldName'], $this->cacheToArray($manyToOneElement['cache'])); } } } // Evaluate manyToMany relationships if (isset($element['manyToMany'])) { foreach ($element['manyToMany'] as $name => $manyToManyElement) { $mapping = array('fieldName' => $name, 'targetEntity' => $manyToManyElement['targetEntity']); if (isset($manyToManyElement['fetch'])) { $mapping['fetch'] = constant('Doctrine\\ORM\\Mapping\\ClassMetadata::FETCH_' . $manyToManyElement['fetch']); } if (isset($manyToManyElement['mappedBy'])) { $mapping['mappedBy'] = $manyToManyElement['mappedBy']; } else { if (isset($manyToManyElement['joinTable'])) { $joinTableElement = $manyToManyElement['joinTable']; $joinTable = array('name' => $joinTableElement['name']); if (isset($joinTableElement['schema'])) { $joinTable['schema'] = $joinTableElement['schema']; } if (isset($joinTableElement['joinColumns'])) { foreach ($joinTableElement['joinColumns'] as $joinColumnName => $joinColumnElement) { if (!isset($joinColumnElement['name'])) { $joinColumnElement['name'] = $joinColumnName; } } $joinTable['joinColumns'][] = $this->joinColumnToArray($joinColumnElement); } if (isset($joinTableElement['inverseJoinColumns'])) { foreach ($joinTableElement['inverseJoinColumns'] as $joinColumnName => $joinColumnElement) { if (!isset($joinColumnElement['name'])) { $joinColumnElement['name'] = $joinColumnName; } } $joinTable['inverseJoinColumns'][] = $this->joinColumnToArray($joinColumnElement); } $mapping['joinTable'] = $joinTable; } } if (isset($manyToManyElement['inversedBy'])) { $mapping['inversedBy'] = $manyToManyElement['inversedBy']; } if (isset($manyToManyElement['cascade'])) { $mapping['cascade'] = $manyToManyElement['cascade']; } if (isset($manyToManyElement['orderBy'])) { $mapping['orderBy'] = $manyToManyElement['orderBy']; } if (isset($manyToManyElement['indexBy'])) { $mapping['indexBy'] = $manyToManyElement['indexBy']; } if (isset($manyToManyElement['orphanRemoval'])) { $mapping['orphanRemoval'] = (bool) $manyToManyElement['orphanRemoval']; } $metadata->mapManyToMany($mapping); // Evaluate second level cache if (isset($manyToManyElement['cache'])) { $metadata->enableAssociationCache($mapping['fieldName'], $this->cacheToArray($manyToManyElement['cache'])); } } } // Evaluate associationOverride if (isset($element['associationOverride']) && is_array($element['associationOverride'])) { foreach ($element['associationOverride'] as $fieldName => $associationOverrideElement) { $override = array(); // Check for joinColumn if (isset($associationOverrideElement['joinColumn'])) { $joinColumns = array(); foreach ($associationOverrideElement['joinColumn'] as $name => $joinColumnElement) { if (!isset($joinColumnElement['name'])) { $joinColumnElement['name'] = $name; } $joinColumns[] = $this->joinColumnToArray($joinColumnElement); } $override['joinColumns'] = $joinColumns; } // Check for joinTable if (isset($associationOverrideElement['joinTable'])) { $joinTableElement = $associationOverrideElement['joinTable']; $joinTable = array('name' => $joinTableElement['name']); if (isset($joinTableElement['schema'])) { $joinTable['schema'] = $joinTableElement['schema']; } foreach ($joinTableElement['joinColumns'] as $name => $joinColumnElement) { if (!isset($joinColumnElement['name'])) { $joinColumnElement['name'] = $name; } $joinTable['joinColumns'][] = $this->joinColumnToArray($joinColumnElement); } foreach ($joinTableElement['inverseJoinColumns'] as $name => $joinColumnElement) { if (!isset($joinColumnElement['name'])) { $joinColumnElement['name'] = $name; } $joinTable['inverseJoinColumns'][] = $this->joinColumnToArray($joinColumnElement); } $override['joinTable'] = $joinTable; } $metadata->setAssociationOverride($fieldName, $override); } } // Evaluate associationOverride if (isset($element['attributeOverride']) && is_array($element['attributeOverride'])) { foreach ($element['attributeOverride'] as $fieldName => $attributeOverrideElement) { $mapping = $this->columnToArray($fieldName, $attributeOverrideElement); $metadata->setAttributeOverride($fieldName, $mapping); } } // Evaluate lifeCycleCallbacks if (isset($element['lifecycleCallbacks'])) { foreach ($element['lifecycleCallbacks'] as $type => $methods) { foreach ($methods as $method) { $metadata->addLifecycleCallback($method, constant('Doctrine\\ORM\\Events::' . $type)); } } } // Evaluate entityListeners if (isset($element['entityListeners'])) { foreach ($element['entityListeners'] as $className => $entityListener) { // Evaluate the listener using naming convention. if (empty($entityListener)) { EntityListenerBuilder::bindEntityListener($metadata, $className); continue; } foreach ($entityListener as $eventName => $callbackElement) { foreach ($callbackElement as $methodName) { $metadata->addEntityListener($eventName, $className, $methodName); } } } } }
/** * Loads the metadata for the specified class into the provided container. * * @param string $className * @param ClassMetadata $metadata * @return void * @throws ORM\MappingException * @throws \UnexpectedValueException */ public function loadMetadataForClass($className, ClassMetadata $metadata) { /** * This is the actual type we have at this point, but we cannot change the * signature due to inheritance. * * @var ORM\ClassMetadata $metadata */ $class = $metadata->getReflectionClass(); $classSchema = $this->getClassSchema($class->getName()); $classAnnotations = $this->reader->getClassAnnotations($class); // Evaluate Entity annotation if (isset($classAnnotations[ORM\MappedSuperclass::class])) { $mappedSuperclassAnnotation = $classAnnotations[ORM\MappedSuperclass::class]; if ($mappedSuperclassAnnotation->repositoryClass !== null) { $metadata->setCustomRepositoryClass($mappedSuperclassAnnotation->repositoryClass); } $metadata->isMappedSuperclass = true; } elseif (isset($classAnnotations[Flow\Entity::class]) || isset($classAnnotations[ORM\Entity::class])) { $entityAnnotation = isset($classAnnotations[Flow\Entity::class]) ? $classAnnotations[Flow\Entity::class] : $classAnnotations[ORM\Entity::class]; if ($entityAnnotation->repositoryClass !== null) { $metadata->setCustomRepositoryClass($entityAnnotation->repositoryClass); } elseif ($classSchema->getRepositoryClassName() !== null) { if ($this->reflectionService->isClassImplementationOf($classSchema->getRepositoryClassName(), \Doctrine\ORM\EntityRepository::class)) { $metadata->setCustomRepositoryClass($classSchema->getRepositoryClassName()); } } if ($entityAnnotation->readOnly) { $metadata->markReadOnly(); } } elseif (isset($classAnnotations[Flow\ValueObject::class])) { $valueObjectAnnotation = $classAnnotations[Flow\ValueObject::class]; if ($valueObjectAnnotation->embedded === true) { $metadata->isEmbeddedClass = true; } else { // also ok... but we make it read-only $metadata->markReadOnly(); } } elseif (isset($classAnnotations[ORM\Embeddable::class])) { $metadata->isEmbeddedClass = true; } else { throw ORM\MappingException::classIsNotAValidEntityOrMappedSuperClass($className); } // Evaluate Table annotation $primaryTable = []; if (isset($classAnnotations[ORM\Table::class])) { $tableAnnotation = $classAnnotations[ORM\Table::class]; $primaryTable = ['name' => $tableAnnotation->name, 'schema' => $tableAnnotation->schema]; if ($tableAnnotation->indexes !== null) { foreach ($tableAnnotation->indexes as $indexAnnotation) { $index = ['columns' => $indexAnnotation->columns]; if (!empty($indexAnnotation->flags)) { $index['flags'] = $indexAnnotation->flags; } if (!empty($indexAnnotation->options)) { $index['options'] = $indexAnnotation->flags; } if (!empty($indexAnnotation->name)) { $primaryTable['indexes'][$indexAnnotation->name] = $index; } else { $primaryTable['indexes'][] = $index; } } } if ($tableAnnotation->uniqueConstraints !== null) { foreach ($tableAnnotation->uniqueConstraints as $uniqueConstraint) { $uniqueConstraint = ['columns' => $uniqueConstraint->columns]; if (!empty($uniqueConstraint->options)) { $uniqueConstraint['options'] = $uniqueConstraint->options; } if (!empty($uniqueConstraint->name)) { $primaryTable['uniqueConstraints'][$uniqueConstraint->name] = $uniqueConstraint; } else { $primaryTable['uniqueConstraints'][] = $uniqueConstraint; } } } if ($tableAnnotation->options !== null) { $primaryTable['options'] = $tableAnnotation->options; } } if (!isset($classAnnotations[ORM\Embeddable::class]) && !isset($primaryTable['name'])) { $className = $classSchema->getClassName(); $primaryTable['name'] = $this->inferTableNameFromClassName($className); } // Evaluate @Cache annotation if (isset($classAnnotations[ORM\Cache::class])) { $cacheAnnotation = $classAnnotations[ORM\Cache::class]; $cacheMap = array('region' => $cacheAnnotation->region, 'usage' => constant('Doctrine\\ORM\\Mapping\\ClassMetadata::CACHE_USAGE_' . $cacheAnnotation->usage)); $metadata->enableCache($cacheMap); } // Evaluate NamedNativeQueries annotation if (isset($classAnnotations[ORM\NamedNativeQueries::class])) { $namedNativeQueriesAnnotation = $classAnnotations[ORM\NamedNativeQueries::class]; foreach ($namedNativeQueriesAnnotation->value as $namedNativeQuery) { $metadata->addNamedNativeQuery(['name' => $namedNativeQuery->name, 'query' => $namedNativeQuery->query, 'resultClass' => $namedNativeQuery->resultClass, 'resultSetMapping' => $namedNativeQuery->resultSetMapping]); } } // Evaluate SqlResultSetMappings annotation if (isset($classAnnotations[ORM\SqlResultSetMappings::class])) { $sqlResultSetMappingsAnnotation = $classAnnotations[ORM\SqlResultSetMappings::class]; foreach ($sqlResultSetMappingsAnnotation->value as $resultSetMapping) { $entities = []; $columns = []; foreach ($resultSetMapping->entities as $entityResultAnnotation) { $entityResult = ['fields' => [], 'entityClass' => $entityResultAnnotation->entityClass, 'discriminatorColumn' => $entityResultAnnotation->discriminatorColumn]; foreach ($entityResultAnnotation->fields as $fieldResultAnnotation) { $entityResult['fields'][] = ['name' => $fieldResultAnnotation->name, 'column' => $fieldResultAnnotation->column]; } $entities[] = $entityResult; } foreach ($resultSetMapping->columns as $columnResultAnnotation) { $columns[] = ['name' => $columnResultAnnotation->name]; } $metadata->addSqlResultSetMapping(['name' => $resultSetMapping->name, 'entities' => $entities, 'columns' => $columns]); } } // Evaluate NamedQueries annotation if (isset($classAnnotations[ORM\NamedQueries::class])) { $namedQueriesAnnotation = $classAnnotations[ORM\NamedQueries::class]; if (!is_array($namedQueriesAnnotation->value)) { throw new \UnexpectedValueException('@ORM\\NamedQueries should contain an array of @ORM\\NamedQuery annotations.'); } foreach ($namedQueriesAnnotation->value as $namedQuery) { if (!$namedQuery instanceof ORM\NamedQuery) { throw new \UnexpectedValueException('@ORM\\NamedQueries should contain an array of @ORM\\NamedQuery annotations.'); } $metadata->addNamedQuery(['name' => $namedQuery->name, 'query' => $namedQuery->query]); } } // Evaluate InheritanceType annotation if (isset($classAnnotations[ORM\InheritanceType::class])) { $inheritanceTypeAnnotation = $classAnnotations[ORM\InheritanceType::class]; $inheritanceType = constant('Doctrine\\ORM\\Mapping\\ClassMetadata::INHERITANCE_TYPE_' . strtoupper($inheritanceTypeAnnotation->value)); if ($inheritanceType !== ORM\ClassMetadata::INHERITANCE_TYPE_NONE) { // Evaluate DiscriminatorColumn annotation if (isset($classAnnotations[ORM\DiscriminatorColumn::class])) { $discriminatorColumnAnnotation = $classAnnotations[ORM\DiscriminatorColumn::class]; $discriminatorColumn = ['name' => $discriminatorColumnAnnotation->name, 'type' => $discriminatorColumnAnnotation->type, 'length' => $discriminatorColumnAnnotation->length, 'columnDefinition' => $discriminatorColumnAnnotation->columnDefinition]; } else { $discriminatorColumn = ['name' => 'dtype', 'type' => 'string', 'length' => 255]; } // Evaluate DiscriminatorMap annotation if (isset($classAnnotations[ORM\DiscriminatorMap::class])) { $discriminatorMapAnnotation = $classAnnotations[ORM\DiscriminatorMap::class]; $discriminatorMap = $discriminatorMapAnnotation->value; } else { $discriminatorMap = []; $subclassNames = $this->reflectionService->getAllSubClassNamesForClass($className); if (!$this->reflectionService->isClassAbstract($className)) { $mappedClassName = strtolower(str_replace('Domain_Model_', '', str_replace('\\', '_', $className))); $discriminatorMap[$mappedClassName] = $className; } foreach ($subclassNames as $subclassName) { $mappedSubclassName = strtolower(str_replace('Domain_Model_', '', str_replace('\\', '_', $subclassName))); $discriminatorMap[$mappedSubclassName] = $subclassName; } } if ($discriminatorMap !== []) { $metadata->setDiscriminatorColumn($discriminatorColumn); $metadata->setDiscriminatorMap($discriminatorMap); } else { $inheritanceType = ORM\ClassMetadata::INHERITANCE_TYPE_NONE; } } $metadata->setInheritanceType($inheritanceType); } // Evaluate DoctrineChangeTrackingPolicy annotation if (isset($classAnnotations[ORM\ChangeTrackingPolicy::class])) { $changeTrackingAnnotation = $classAnnotations[ORM\ChangeTrackingPolicy::class]; $metadata->setChangeTrackingPolicy(constant('Doctrine\\ORM\\Mapping\\ClassMetadata::CHANGETRACKING_' . strtoupper($changeTrackingAnnotation->value))); } else { $metadata->setChangeTrackingPolicy(ORM\ClassMetadata::CHANGETRACKING_DEFERRED_EXPLICIT); } // Evaluate annotations on properties/fields try { $this->evaluatePropertyAnnotations($metadata); } catch (ORM\MappingException $exception) { throw new ORM\MappingException(sprintf('Failure while evaluating property annotations for class "%s": %s', $metadata->getName(), $exception->getMessage()), 1382003497, $exception); } // build unique index for table if (!isset($classAnnotations[ORM\Embeddable::class]) && !isset($primaryTable['uniqueConstraints'])) { $idProperties = array_keys($classSchema->getIdentityProperties()); if (array_diff($idProperties, $metadata->getIdentifierFieldNames()) !== []) { $uniqueIndexName = $this->truncateIdentifier('flow_identity_' . $primaryTable['name']); foreach ($idProperties as $idProperty) { $primaryTable['uniqueConstraints'][$uniqueIndexName]['columns'][] = isset($metadata->columnNames[$idProperty]) ? $metadata->columnNames[$idProperty] : strtolower($idProperty); } } } $metadata->setPrimaryTable($primaryTable); // Evaluate AssociationOverrides annotation $this->evaluateOverridesAnnotations($classAnnotations, $metadata); // Evaluate EntityListeners annotation $this->evaluateEntityListenersAnnotation($class, $metadata, $classAnnotations); // Evaluate @HasLifecycleCallbacks annotation $this->evaluateLifeCycleAnnotations($class, $metadata); }