1
0
Fork 0
mirror of synced 2025-04-03 13:23:37 +03:00

[2.0] Updating YAML and XML drivers to be synchronized with Annotations driver.

This commit is contained in:
jwage 2009-08-24 21:05:55 +00:00
parent 27356225dd
commit fd89892cc9
7 changed files with 431 additions and 332 deletions

View file

@ -33,7 +33,7 @@
</xs:sequence> </xs:sequence>
</xs:complexType> </xs:complexType>
<xs:simpleType name="lifecycle-callback-type"> <xs:simpleType name="lifecycle-listener-type">
<xs:restriction base="xs:token"> <xs:restriction base="xs:token">
<xs:enumeration value="prePersist"/> <xs:enumeration value="prePersist"/>
<xs:enumeration value="postPersist"/> <xs:enumeration value="postPersist"/>
@ -45,14 +45,14 @@
</xs:restriction> </xs:restriction>
</xs:simpleType> </xs:simpleType>
<xs:complexType name="lifecycle-callback"> <xs:complexType name="lifecycle-listener">
<xs:attribute name="type" type="orm:lifecycle-callback-type" use="required" /> <xs:attribute name="type" type="orm:lifecycle-listener-type" use="required" />
<xs:attribute name="method" type="xs:NMTOKEN" use="required" /> <xs:attribute name="method" type="xs:NMTOKEN" use="required" />
</xs:complexType> </xs:complexType>
<xs:complexType name="lifecycle-callbacks"> <xs:complexType name="lifecycle-listener">
<xs:sequence> <xs:sequence>
<xs:element name="lifecycle-callback" type="orm:lifecycle-callback" minOccurs="1" maxOccurs="unbounded" /> <xs:element name="lifecycle-listener" type="orm:lifecycle-listener" minOccurs="1" maxOccurs="unbounded" />
</xs:sequence> </xs:sequence>
</xs:complexType> </xs:complexType>

View file

@ -112,12 +112,6 @@ class AnnotationDriver implements Driver
$metadata->setDiscriminatorMap($discrMapAnnot->value); $metadata->setDiscriminatorMap($discrMapAnnot->value);
} }
// Evaluate DoctrineSubClasses annotation
if (isset($classAnnotations['Doctrine\ORM\Mapping\SubClasses'])) {
$subClassesAnnot = $classAnnotations['Doctrine\ORM\Mapping\SubClasses'];
$metadata->setSubclasses($subClassesAnnot->value);
}
// Evaluate DoctrineChangeTrackingPolicy annotation // Evaluate DoctrineChangeTrackingPolicy annotation
if (isset($classAnnotations['Doctrine\ORM\Mapping\ChangeTrackingPolicy'])) { if (isset($classAnnotations['Doctrine\ORM\Mapping\ChangeTrackingPolicy'])) {
$changeTrackingAnnot = $classAnnotations['Doctrine\ORM\Mapping\ChangeTrackingPolicy']; $changeTrackingAnnot = $classAnnotations['Doctrine\ORM\Mapping\ChangeTrackingPolicy'];

View file

@ -41,193 +41,248 @@ class XmlDriver extends AbstractFileDriver
*/ */
public function loadMetadataForClass($className, ClassMetadata $metadata) public function loadMetadataForClass($className, ClassMetadata $metadata)
{ {
$class = $metadata->getReflectionClass();
$xmlRoot = $this->getElement($className); $xmlRoot = $this->getElement($className);
if ($xmlRoot->getName() == 'entity') { if ($xmlRoot->getName() == 'entity') {
$metadata->setCustomRepositoryClass(
isset($xmlRoot['repository-class']) ? $xmlRoot['repository-class'] : null
);
} else if ($xmlRoot->getName() == 'mapped-superclass') {
$metadata->isMappedSuperclass = true;
} else {
throw DoctrineException::updateMe("$className is no entity or mapped superclass.");
}
// Evaluate <entity...> attributes // Evaluate <entity...> attributes
if (isset($xmlRoot['table'])) { if (isset($xmlRoot['table'])) {
$metadata->primaryTable['name'] = (string)$xmlRoot['table']; $metadata->primaryTable['name'] = (string)$xmlRoot['table'];
} }
if (isset($xmlRoot['schema'])) { if (isset($xmlRoot['schema'])) {
$metadata->primaryTable['schema'] = (string)$xmlRoot['schema']; $metadata->primaryTable['schema'] = (string)$xmlRoot['schema'];
} }
if (isset($xmlRoot['inheritance-type'])) { if (isset($xmlRoot['inheritance-type'])) {
$metadata->setInheritanceType((string)$xmlRoot['inheritance-type']); $metadata->setInheritanceType((string)$xmlRoot['inheritance-type']);
} }
// Evaluate <indexes...>
if (isset($xmlRoot->indexes)) {
foreach ($xmlRoot->indexes->index as $index) {
$metadata->primaryTable['indexes'][$index['name']] = array('fields' =>
explode(',', $index['columns']));
}
}
// Evaluate <unique-constraints..>
if (isset($xmlRoot->{'unique-constraints'})) {
foreach ($xmlRoot->{'unique-constraints'}->{'unique-constraint'} as $unique) {
$metadata->primaryTable['uniqueConstraints'][] = explode(',', $unique['columns']);
}
}
// Evaluate <field ...> mappings // Evaluate <discriminator-column...>
if (isset($xmlRoot->field)) { if (isset($xmlRoot->{'discriminator-column'})) {
foreach ($xmlRoot->field as $fieldMapping) { $discrColumn = $xmlRoot->{'discriminator-column'};
$mapping = array( $metadata->setDiscriminatorColumn(array(
'fieldName' => (string)$fieldMapping['name'], 'name' => (string)$discrColumn->name,
'type' => (string)$fieldMapping['type'] 'type' => (string)$discrColumn->type,
); 'length' => (string)$discrColumn->length
if (isset($fieldMapping['column'])) { ));
$mapping['columnName'] = (string)$fieldMapping['column']; }
}
if (isset($fieldMapping['length'])) {
$mapping['length'] = (int)$fieldMapping['length'];
}
if (isset($fieldMapping['precision'])) {
$mapping['precision'] = (int)$fieldMapping['precision'];
}
if (isset($fieldMapping['scale'])) {
$mapping['scale'] = (int)$fieldMapping['scale'];
}
if (isset($fieldMapping['version']) && $fieldMapping['version']) {
$metadata->setVersionMapping($mapping);
}
$metadata->mapField($mapping);
}
}
// Evaluate <id ...> mappings // Evaluate <discriminator-map...>
foreach ($xmlRoot->id as $idElement) { if (isset($xmlRoot->{'discriminator-map'})) {
$metadata->setDiscriminatorMap((array)$xmlRoot->{'discriminator-map'});
}
// Evaluate <change-tracking-policy...>
if (isset($xmlRoot->{'change-tracking-policy'})) {
$metadata->setChangeTrackingPolicy((array)$xmlRoot->{'change-tracking-policy'});
}
// Evaluate <indexes...>
if (isset($xmlRoot->indexes)) {
foreach ($xmlRoot->indexes->index as $index) {
$metadata->primaryTable['indexes'][$index['name']] = array('fields' =>
explode(',', $index['columns']));
}
}
// Evaluate <unique-constraints..>
if (isset($xmlRoot->{'unique-constraints'})) {
foreach ($xmlRoot->{'unique-constraints'}->{'unique-constraint'} as $unique) {
$metadata->primaryTable['uniqueConstraints'][] = explode(',', $unique['columns']);
}
}
// Evaluate <field ...> mappings
if (isset($xmlRoot->field)) {
foreach ($xmlRoot->field as $fieldMapping) {
$mapping = array( $mapping = array(
'id' => true, 'fieldName' => (string)$fieldMapping['name'],
'fieldName' => (string)$idElement['name'], 'type' => (string)$fieldMapping['type']
'type' => (string)$idElement['type']
); );
if (isset($idElement['column'])) { if (isset($fieldMapping['column'])) {
$mapping['columnName'] = (string)$idElement['column']; $mapping['columnName'] = (string)$fieldMapping['column'];
}
if (isset($fieldMapping['length'])) {
$mapping['length'] = (int)$fieldMapping['length'];
}
if (isset($fieldMapping['precision'])) {
$mapping['precision'] = (int)$fieldMapping['precision'];
}
if (isset($fieldMapping['scale'])) {
$mapping['scale'] = (int)$fieldMapping['scale'];
}
if (isset($fieldMapping['version']) && $fieldMapping['version']) {
$metadata->setVersionMapping($mapping);
} }
$metadata->mapField($mapping); $metadata->mapField($mapping);
}
}
if (isset($idElement->generator)) { // Evaluate <id ...> mappings
$metadata->setIdGeneratorType(constant('Doctrine\ORM\Mapping\ClassMetadata::GENERATOR_TYPE_' foreach ($xmlRoot->id as $idElement) {
. (string)$idElement->generator['strategy'])); $mapping = array(
} 'id' => true,
'fieldName' => (string)$idElement['name'],
'type' => (string)$idElement['type']
);
if (isset($idElement['column'])) {
$mapping['columnName'] = (string)$idElement['column'];
}
$metadata->mapField($mapping);
if (isset($idElement->generator)) {
$metadata->setIdGeneratorType(constant('Doctrine\ORM\Mapping\ClassMetadata::GENERATOR_TYPE_'
. (string)$idElement->generator['strategy']));
} }
// Evaluate <one-to-one ...> mappings // Check for SequenceGenerator/TableGenerator definition
if (isset($xmlRoot->{'one-to-one'})) { if (isset($idElement->{'sequence-generator'})) {
foreach ($xmlRoot->{'one-to-one'} as $oneToOneElement) { $seqGenerator = $idElement->{'sequence-generator'};
$mapping = array( $metadata->setSequenceGeneratorDefinition(array(
'fieldName' => (string)$oneToOneElement['field'], 'sequenceName' => $seqGenerator->{'sequence-name'},
'targetEntity' => (string)$oneToOneElement['target-entity'] 'allocationSize' => $seqGenerator->{'allocation-size'},
); 'initialValue' => $seqGeneratorAnnot->{'initial-value'}
if (isset($oneToOneElement['mapped-by'])) { ));
$mapping['mappedBy'] = (string)$oneToOneElement['mapped-by']; } else if (isset($idElement->{'table-generator'})) {
} else { throw DoctrineException::tableIdGeneratorNotImplemented();
$joinColumns = array();
if (isset($oneToOneElement->{'join-column'})) {
$joinColumns[] = $this->_getJoinColumnMapping($oneToOneElement->{'join-column'});
} else if (isset($oneToOneElement->{'join-columns'})) {
foreach ($oneToOneElement->{'join-columns'}->{'join-column'} as $joinColumnElement) {
$joinColumns[] = $this->_getJoinColumnMapping($joinColumnElement);
}
} else {
throw MappingException::invalidMapping($mapping['fieldName']);
}
$mapping['joinColumns'] = $joinColumns;
}
if (isset($oneToOneElement->cascade)) {
$mapping['cascade'] = $this->_getCascadeMappings($oneToOneElement->cascade);
}
$metadata->mapOneToOne($mapping);
}
} }
}
// Evaluate <one-to-many ...> mappings // Evaluate <one-to-one ...> mappings
if (isset($xmlRoot->{'one-to-many'})) { if (isset($xmlRoot->{'one-to-one'})) {
foreach ($xmlRoot->{'one-to-many'} as $oneToManyElement) { foreach ($xmlRoot->{'one-to-one'} as $oneToOneElement) {
$mapping = array( $mapping = array(
'fieldName' => (string)$oneToManyElement['field'], 'fieldName' => (string)$oneToOneElement['field'],
'targetEntity' => (string)$oneToManyElement['target-entity'], 'targetEntity' => (string)$oneToOneElement['target-entity'],
'mappedBy' => (string)$oneToManyElement['mapped-by'] );
); if (isset($oneToOneElement['mapped-by'])) {
if (isset($oneToManyElement->cascade)) { $mapping['mappedBy'] = (string)$oneToOneElement['mapped-by'];
$mapping['cascade'] = $this->_getCascadeMappings($oneToManyElement->cascade); } else {
}
$metadata->mapOneToMany($mapping);
}
}
// 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']
);
$joinColumns = array(); $joinColumns = array();
if (isset($manyToOneElement->{'join-column'})) { if (isset($oneToOneElement->{'join-column'})) {
$joinColumns[] = $this->_getJoinColumnMapping($manyToOneElement->{'join-column'}); $joinColumns[] = $this->_getJoinColumnMapping($oneToOneElement->{'join-column'});
} else if (isset($manyToOneElement->{'join-columns'})) { } else if (isset($oneToOneElement->{'join-columns'})) {
foreach ($manyToOneElement->{'join-columns'}->{'join-column'} as $joinColumnElement) { foreach ($oneToOneElement->{'join-columns'}->{'join-column'} as $joinColumnElement) {
if (!isset($joinColumnElement['name'])) {
$joinColumnElement['name'] = $name;
}
$joinColumns[] = $this->_getJoinColumnMapping($joinColumnElement); $joinColumns[] = $this->_getJoinColumnMapping($joinColumnElement);
} }
} else { } else {
throw MappingException::invalidMapping($mapping['fieldName']); throw MappingException::invalidMapping($mapping['fieldName']);
} }
$mapping['joinColumns'] = $joinColumns; $mapping['joinColumns'] = $joinColumns;
if (isset($manyToOneElement->cascade)) {
$mapping['cascade'] = $this->_getCascadeMappings($manyToOneElement->cascade);
}
$metadata->mapManyToOne($mapping);
} }
if (isset($oneToOneElement->cascade)) {
$mapping['cascade'] = $this->_getCascadeMappings($oneToOneElement->cascade);
}
if (isset($oneToOneElement->{'orphan-removal'})) {
$mapping['orphanRemoval'] = (bool)$oneToOneElement->{'orphan-removal'};
}
$metadata->mapOneToOne($mapping);
} }
}
// Evaluate <many-to-many ...> mappings
if (isset($xmlRoot->{'many-to-many'})) { // Evaluate <one-to-many ...> mappings
foreach ($xmlRoot->{'many-to-many'} as $manyToManyElement) { if (isset($xmlRoot->{'one-to-many'})) {
$mapping = array( foreach ($xmlRoot->{'one-to-many'} as $oneToManyElement) {
'fieldName' => (string)$manyToManyElement['field'], $mapping = array(
'targetEntity' => (string)$manyToManyElement['target-entity'] 'fieldName' => (string)$oneToManyElement['field'],
'targetEntity' => (string)$oneToManyElement['target-entity'],
'mappedBy' => (string)$oneToManyElement['mapped-by']
);
if (isset($oneToManyElement->cascade)) {
$mapping['cascade'] = $this->_getCascadeMappings($oneToManyElement->cascade);
}
if (isset($oneToManyElement->{'orphan-removal'})) {
$mapping['orphanRemoval'] = (bool)$oneToManyElement->{'orphan-removal'};
}
$metadata->mapOneToMany($mapping);
}
}
// 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']
);
$joinColumns = array();
if (isset($manyToOneElement->{'join-column'})) {
$joinColumns[] = $this->_getJoinColumnMapping($manyToOneElement->{'join-column'});
} else if (isset($manyToOneElement->{'join-columns'})) {
foreach ($manyToOneElement->{'join-columns'}->{'join-column'} as $joinColumnElement) {
if (!isset($joinColumnElement['name'])) {
$joinColumnElement['name'] = $name;
}
$joinColumns[] = $this->_getJoinColumnMapping($joinColumnElement);
}
} else {
throw MappingException::invalidMapping($mapping['fieldName']);
}
$mapping['joinColumns'] = $joinColumns;
if (isset($manyToOneElement->cascade)) {
$mapping['cascade'] = $this->_getCascadeMappings($manyToOneElement->cascade);
}
if (isset($manyToOneElement->{'orphan-removal'})) {
$mapping['orphanRemoval'] = (bool)$manyToOneElement->{'orphan-removal'};
}
$metadata->mapManyToOne($mapping);
}
}
// 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['mappedBy'])) {
$mapping['mappedBy'] = (string)$manyToManyElement['mapped-by'];
} else if (isset($manyToManyElement->{'join-table'})) {
$joinTableElement = $manyToManyElement->{'join-table'};
$joinTable = array(
'name' => (string)$joinTableElement['name']
); );
if (isset($joinTableElement['schema'])) {
if (isset($manyToManyElement['mappedBy'])) { $joinTable['schema'] = (string)$joinTableElement['schema'];
$mapping['mappedBy'] = (string)$manyToManyElement['mapped-by'];
} else if (isset($manyToManyElement->{'join-table'})) {
$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->_getJoinColumnMapping($joinColumnElement);
}
foreach ($joinTableElement->{'inverse-join-columns'}->{'join-column'} as $joinColumnElement) {
$joinTable['inverseJoinColumns'][] = $this->_getJoinColumnMapping($joinColumnElement);
}
$mapping['joinTable'] = $joinTable;
} else {
throw MappingException::invalidMapping($mapping['fieldName']);
} }
foreach ($joinTableElement->{'join-columns'}->{'join-column'} as $joinColumnElement) {
if (isset($manyToManyElement->cascade)) { $joinTable['joinColumns'][] = $this->_getJoinColumnMapping($joinColumnElement);
$mapping['cascade'] = $this->_getCascadeMappings($manyToManyElement->cascade);
} }
foreach ($joinTableElement->{'inverse-join-columns'}->{'join-column'} as $joinColumnElement) {
$joinTable['inverseJoinColumns'][] = $this->_getJoinColumnMapping($joinColumnElement);
}
$mapping['joinTable'] = $joinTable;
} else {
throw MappingException::invalidMapping($mapping['fieldName']);
}
if (isset($manyToManyElement->cascade)) {
$mapping['cascade'] = $this->_getCascadeMappings($manyToManyElement->cascade);
}
if (isset($manyToManyElement->{'orphan-removal'})) {
$mapping['orphanRemoval'] = (bool)$manyToManyElement->{'orphan-removal'};
}
$metadata->mapManyToMany($mapping);
}
}
$metadata->mapManyToMany($mapping); // Evaluate <lifecycle-listener...>
if (isset($xmlRoot->{'lifecycle-listener'})) {
foreach ($xmlRoot->{'lifecycle-listener'} as $lifecycleListener) {
$method = $class->getMethod((string)$lifecycleListener['method']);
if ($method->isPublic()) {
$metadata->addLifecycleCallback($method->getName(), constant('\Doctrine\ORM\Events::' . (string)$lifecycleListener['type']));
} }
} }
} else if ($xmlRoot->getName() == 'mapped-superclass') {
throw MappingException::notImplemented('Mapped superclasses are not yet supported.');
} }
} }

View file

@ -42,141 +42,125 @@ class YamlDriver extends AbstractFileDriver
public function loadMetadataForClass($className, ClassMetadata $metadata) public function loadMetadataForClass($className, ClassMetadata $metadata)
{ {
$class = $metadata->getReflectionClass();
$element = $this->getElement($className); $element = $this->getElement($className);
if ($element['type'] == 'entity') { if ($element['type'] == 'entity') {
$metadata->setCustomRepositoryClass(
isset($element['repositoryClass']) ? $xmlRoot['repositoryClass'] : null
);
} else if ($element['type'] == 'mappedSuperclass') {
$metadata->isMappedSuperclass = true;
} else {
throw DoctrineException::updateMe("$className is no entity or mapped superclass.");
}
// Evaluate root level properties // Evaluate root level properties
if (isset($element['table'])) { if (isset($element['table'])) {
$metadata->primaryTable['name'] = $element['table']; $metadata->primaryTable['name'] = $element['table'];
} }
if (isset($element['schema'])) { if (isset($element['schema'])) {
$metadata->primaryTable['schema'] = $element['schema']; $metadata->primaryTable['schema'] = $element['schema'];
} }
if (isset($element['inheritanceType'])) { if (isset($element['inheritanceType'])) {
$metadata->setInheritanceType($element['inheritanceType']); $metadata->setInheritanceType($element['inheritanceType']);
} }
// Evaluate indexes // Evaluate discriminatorColumn
if (isset($element['indexes'])) { if (isset($element['discriminatorColumn'])) {
foreach ($element['indexes'] as $index) { $discrColumn = $element['discriminatorColumn'];
$metadata->primaryTable['indexes'][$index['name']] = array('fields' => $metadata->setDiscriminatorColumn(array(
explode(',', $index['columns'])); 'name' => $discrColumn['name'],
} 'type' => $discrColumn['type'],
} 'length' => $discrColumn['length']
));
}
// Evaluate uniqueConstraints // Evaluate discriminatorMap
if (isset($element['uniqueConstraints'])) { if (isset($element['discriminatorMap'])) {
foreach ($element['uniqueConstraints'] as $unique) { $metadata->setDiscriminatorMap($element['discriminatorMap']);
$metadata->primaryTable['uniqueConstraints'][] = explode(',', $unique['columns']); }
}
}
// Evaluate fields // Evaluate changeTrackingPolicy
if (isset($element['fields'])) { if (isset($element['changeTrackingPolicy'])) {
foreach ($element['fields'] as $name => $fieldMapping) { $metadata->setChangeTrackingPolicy($element['changeTrackingPolicy']);
$mapping = array( }
'fieldName' => $name,
'type' => $fieldMapping['type']
);
if (isset($fieldMapping['column'])) {
$mapping['columnName'] = $fieldMapping['column'];
}
if (isset($fieldMapping['length'])) {
$mapping['length'] = $fieldMapping['length'];
}
if (isset($fieldMapping['precision'])) {
$mapping['precision'] = $fieldMapping['precision'];
}
if (isset($fieldMapping['scale'])) {
$mapping['scale'] = $fieldMapping['scale'];
}
if (isset($fieldMapping['version']) && $fieldMapping['version']) {
$metadata->setVersionMapping($mapping);
}
$metadata->mapField($mapping);
}
}
// Evaluate identifier settings // Evaluate indexes
foreach ($element['id'] as $name => $idElement) { if (isset($element['indexes'])) {
foreach ($element['indexes'] as $index) {
$metadata->primaryTable['indexes'][$index['name']] = array('fields' =>
explode(',', $index['columns']));
}
}
// Evaluate uniqueConstraints
if (isset($element['uniqueConstraints'])) {
foreach ($element['uniqueConstraints'] as $unique) {
$metadata->primaryTable['uniqueConstraints'][] = explode(',', $unique['columns']);
}
}
// Evaluate fields
if (isset($element['fields'])) {
foreach ($element['fields'] as $name => $fieldMapping) {
$mapping = array( $mapping = array(
'id' => true,
'fieldName' => $name, 'fieldName' => $name,
'type' => $idElement['type'] 'type' => $fieldMapping['type']
); );
if (isset($idElement['column'])) { if (isset($fieldMapping['column'])) {
$mapping['columnName'] = $idElement['column']; $mapping['columnName'] = $fieldMapping['column'];
}
if (isset($fieldMapping['length'])) {
$mapping['length'] = $fieldMapping['length'];
}
if (isset($fieldMapping['precision'])) {
$mapping['precision'] = $fieldMapping['precision'];
}
if (isset($fieldMapping['scale'])) {
$mapping['scale'] = $fieldMapping['scale'];
}
if (isset($fieldMapping['version']) && $fieldMapping['version']) {
$metadata->setVersionMapping($mapping);
} }
$metadata->mapField($mapping); $metadata->mapField($mapping);
if (isset($idElement['generator'])) {
$metadata->setIdGeneratorType(constant('Doctrine\ORM\Mapping\ClassMetadata::GENERATOR_TYPE_'
. $idElement['generator']['strategy']));
}
} }
}
// Evaluate oneToOne relationships // Evaluate identifier settings
if (isset($element['oneToOne'])) { foreach ($element['id'] as $name => $idElement) {
foreach ($element['oneToOne'] as $name => $oneToOneElement) { $mapping = array(
$mapping = array( 'id' => true,
'fieldName' => $name, 'fieldName' => $name,
'targetEntity' => $oneToOneElement['targetEntity'] 'type' => $idElement['type']
); );
if (isset($oneToOneElement['mappedBy'])) { if (isset($idElement['column'])) {
$mapping['mappedBy'] = $oneToOneElement['mappedBy']; $mapping['columnName'] = $idElement['column'];
} else {
$joinColumns = array();
if (isset($oneToOneElement['joinColumn'])) {
$joinColumns[] = $this->_getJoinColumnMapping($oneToOneElement['joinColumn']);
} else if (isset($oneToOneElement['joinColumns'])) {
foreach ($oneToOneElement['joinColumns'] as $name => $joinColumnElement) {
if (!isset($joinColumnElement['name'])) {
$joinColumnElement['name'] = $name;
}
$joinColumns[] = $this->_getJoinColumnMapping($joinColumnElement);
}
} else {
throw MappingException::invalidMapping($mapping['fieldName']);
}
$mapping['joinColumns'] = $joinColumns;
}
if (isset($oneToOneElement['cascade'])) {
$mapping['cascade'] = $this->_getCascadeMappings($oneToOneElement['cascade']);
}
$metadata->mapOneToOne($mapping);
}
} }
$metadata->mapField($mapping);
// Evaluate oneToMany relationships if (isset($idElement['generator'])) {
if (isset($element['oneToMany'])) { $metadata->setIdGeneratorType(constant('Doctrine\ORM\Mapping\ClassMetadata::GENERATOR_TYPE_'
foreach ($element['oneToMany'] as $name => $oneToManyElement) { . $idElement['generator']['strategy']));
$mapping = array(
'fieldName' => $name,
'targetEntity' => $oneToManyElement['targetEntity'],
'mappedBy' => $oneToManyElement['mappedBy']
);
if (isset($oneToManyElement['cascade'])) {
$mapping['cascade'] = $this->_getCascadeMappings($oneToManyElement['cascade']);
}
$metadata->mapOneToMany($mapping);
}
} }
}
// Evaluate manyToOne relationships // Evaluate oneToOne relationships
if (isset($element['manyToOne'])) { if (isset($element['oneToOne'])) {
foreach ($element['manyToOne'] as $name => $manyToOneElement) { foreach ($element['oneToOne'] as $name => $oneToOneElement) {
$mapping = array( $mapping = array(
'fieldName' => $name, 'fieldName' => $name,
'targetEntity' => $manyToOneElement['targetEntity'] 'targetEntity' => $oneToOneElement['targetEntity']
); );
if (isset($oneToOneElement['mappedBy'])) {
$mapping['mappedBy'] = $oneToOneElement['mappedBy'];
} else {
$joinColumns = array(); $joinColumns = array();
if (isset($manyToOneElement['joinColumn'])) { if (isset($oneToOneElement['joinColumn'])) {
$joinColumns[] = $this->_getJoinColumnMapping($manyToOneElement['joinColumn']); $joinColumns[] = $this->_getJoinColumnMapping($oneToOneElement['joinColumn']);
} else if (isset($manyToOneElement['joinColumns'])) { } else if (isset($oneToOneElement['joinColumns'])) {
foreach ($manyToOneElement['joinColumns'] as $name => $joinColumnElement) { foreach ($oneToOneElement['joinColumns'] as $name => $joinColumnElement) {
if (!isset($joinColumnElement['name'])) { if (!isset($joinColumnElement['name'])) {
$joinColumnElement['name'] = $name; $joinColumnElement['name'] = $name;
} }
@ -186,58 +170,110 @@ class YamlDriver extends AbstractFileDriver
throw MappingException::invalidMapping($mapping['fieldName']); throw MappingException::invalidMapping($mapping['fieldName']);
} }
$mapping['joinColumns'] = $joinColumns; $mapping['joinColumns'] = $joinColumns;
if (isset($manyToOneElement['cascade'])) {
$mapping['cascade'] = $this->_getCascadeMappings($manyToOneElement['cascade']);
}
$metadata->mapManyToOne($mapping);
} }
}
// Evaluate manyToMany relationships if (isset($oneToOneElement['cascade'])) {
if (isset($element['manyToMany'])) { $mapping['cascade'] = $this->_getCascadeMappings($oneToOneElement['cascade']);
foreach ($element['manyToMany'] as $name => $manyToManyElement) { }
$mapping = array(
'fieldName' => $name, $metadata->mapOneToOne($mapping);
'targetEntity' => $manyToManyElement['targetEntity'] }
}
// 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['cascade'])) {
$mapping['cascade'] = $this->_getCascadeMappings($oneToManyElement['cascade']);
}
$metadata->mapOneToMany($mapping);
}
}
// Evaluate manyToOne relationships
if (isset($element['manyToOne'])) {
foreach ($element['manyToOne'] as $name => $manyToOneElement) {
$mapping = array(
'fieldName' => $name,
'targetEntity' => $manyToOneElement['targetEntity']
);
$joinColumns = array();
if (isset($manyToOneElement['joinColumn'])) {
$joinColumns[] = $this->_getJoinColumnMapping($manyToOneElement['joinColumn']);
} else if (isset($manyToOneElement['joinColumns'])) {
foreach ($manyToOneElement['joinColumns'] as $name => $joinColumnElement) {
if (!isset($joinColumnElement['name'])) {
$joinColumnElement['name'] = $name;
}
$joinColumns[] = $this->_getJoinColumnMapping($joinColumnElement);
}
} else {
throw MappingException::invalidMapping($mapping['fieldName']);
}
$mapping['joinColumns'] = $joinColumns;
if (isset($manyToOneElement['cascade'])) {
$mapping['cascade'] = $this->_getCascadeMappings($manyToOneElement['cascade']);
}
$metadata->mapManyToOne($mapping);
}
}
// Evaluate manyToMany relationships
if (isset($element['manyToMany'])) {
foreach ($element['manyToMany'] as $name => $manyToManyElement) {
$mapping = array(
'fieldName' => $name,
'targetEntity' => $manyToManyElement['targetEntity']
);
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'])) {
if (isset($manyToManyElement['mappedBy'])) { $joinTable['schema'] = $joinTableElement['schema'];
$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'];
}
foreach ($joinTableElement['joinColumns'] as $name => $joinColumnElement) {
if (!isset($joinColumnElement['name'])) {
$joinColumnElement['name'] = $name;
}
$joinTable['joinColumns'][] = $this->_getJoinColumnMapping($joinColumnElement);
}
foreach ($joinTableElement['inverseJoinColumns'] as $name => $joinColumnElement) {
if (!isset($joinColumnElement['name'])) {
$joinColumnElement['name'] = $name;
}
$joinTable['inverseJoinColumns'][] = $this->_getJoinColumnMapping($joinColumnElement);
}
$mapping['joinTable'] = $joinTable;
} else {
throw MappingException::invalidMapping($mapping['fieldName']);
} }
foreach ($joinTableElement['joinColumns'] as $name => $joinColumnElement) {
if (isset($manyToManyElement['cascade'])) { if (!isset($joinColumnElement['name'])) {
$mapping['cascade'] = $this->_getCascadeMappings($manyToManyElement['cascade']); $joinColumnElement['name'] = $name;
}
$joinTable['joinColumns'][] = $this->_getJoinColumnMapping($joinColumnElement);
} }
foreach ($joinTableElement['inverseJoinColumns'] as $name => $joinColumnElement) {
if (!isset($joinColumnElement['name'])) {
$joinColumnElement['name'] = $name;
}
$joinTable['inverseJoinColumns'][] = $this->_getJoinColumnMapping($joinColumnElement);
}
$mapping['joinTable'] = $joinTable;
} else {
throw MappingException::invalidMapping($mapping['fieldName']);
}
if (isset($manyToManyElement['cascade'])) {
$mapping['cascade'] = $this->_getCascadeMappings($manyToManyElement['cascade']);
}
$metadata->mapManyToMany($mapping); $metadata->mapManyToMany($mapping);
}
}
// Evaluate lifeCycleListener
if (isset($element['lifecycleListeners'])) {
foreach ($element['lifecycleListeners'] as $method => $type) {
$method = $class->getMethod($method);
if ($method->isPublic()) {
$metadata->addLifecycleCallback($method->getName(), constant('\Doctrine\ORM\Events::'.$type));
} }
} }
} else if ($element['type'] == 'mapped-superclass') {
throw MappingException::notImplemented('Mapped superclasses are not yet supported.');
} }
} }

View file

@ -97,6 +97,9 @@ class MappingDriverTest extends \Doctrine\Tests\OrmTestCase
$this->assertTrue($class->associationMappings['groups'] instanceof \Doctrine\ORM\Mapping\ManyToManyMapping); $this->assertTrue($class->associationMappings['groups'] instanceof \Doctrine\ORM\Mapping\ManyToManyMapping);
$this->assertTrue(isset($class->associationMappings['groups'])); $this->assertTrue(isset($class->associationMappings['groups']));
$this->assertTrue($class->associationMappings['groups']->isOwningSide); $this->assertTrue($class->associationMappings['groups']->isOwningSide);
$this->assertEquals(count($class->lifecycleCallbacks), 2);
$this->assertEquals($class->lifecycleCallbacks['prePersist'][0], 'doStuffOnPrePersist');
$this->assertEquals($class->lifecycleCallbacks['postPersist'][0], 'doStuffOnPostPersist');
} }
} }
@ -108,4 +111,13 @@ class User {
private $groups; private $groups;
// ... rest of code omitted, irrelevant for the mapping tests // ... rest of code omitted, irrelevant for the mapping tests
public function doStuffOnPrePersist()
{
}
public function doStuffOnPostPersist()
{
}
} }

View file

@ -7,10 +7,9 @@
<entity name="Doctrine\Tests\ORM\Mapping\User" table="cms_users"> <entity name="Doctrine\Tests\ORM\Mapping\User" table="cms_users">
<lifecycle-callbacks> <lifecycle-listener method="doStuffOnPrePersist" type="prePersist" />
<lifecycle-callback type="prePersist" method="onPrePersist" /> <lifecycle-listener method="doStuffOnPostPersist" type="postPersist" />
</lifecycle-callbacks>
<id name="id" type="integer" column="id"> <id name="id" type="integer" column="id">
<generator strategy="AUTO"/> <generator strategy="AUTO"/>
</id> </id>

View file

@ -31,4 +31,7 @@ Doctrine\Tests\ORM\Mapping\User:
referencedColumnName: id referencedColumnName: id
inverseJoinColumns: inverseJoinColumns:
group_id: group_id:
referencedColumnName: id referencedColumnName: id
lifecycleListeners:
doStuffOnPrePersist: prePersist
doStuffOnPostPersist: postPersist