1
0
mirror of synced 2025-01-30 20:11:49 +03:00

[2.0] Fixing spacing and adding precision and scale in Annotations driver. Also introduced options item in Annotation and Yaml mapping drivers. Missing XML driver implementation.

This commit is contained in:
guilhermeblanco 2009-09-04 18:35:40 +00:00
parent 203b46dea2
commit eb25422617
4 changed files with 117 additions and 5 deletions

View File

@ -77,16 +77,19 @@ class AnnotationDriver implements Driver
'name' => $tableAnnot->name,
'schema' => $tableAnnot->schema
);
if ($tableAnnot->indexes !== null) {
foreach ($tableAnnot->indexes as $indexAnnot) {
$primaryTable['indexes'][$indexAnnot->name] = array('fields' => $indexAnnot->columns);
}
}
if ($tableAnnot->uniqueConstraints !== null) {
foreach ($tableAnnot->uniqueConstraints as $uniqueConstraint) {
$primaryTable['uniqueConstraints'][] = $uniqueConstraint->columns;
}
}
$metadata->setPrimaryTable($primaryTable);
}
@ -129,6 +132,7 @@ class AnnotationDriver implements Driver
// Check for JoinColummn/JoinColumns annotations
$joinColumns = array();
if ($joinColumnAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\JoinColumn')) {
$joinColumns[] = array(
'name' => $joinColumnAnnot->name,
@ -157,24 +161,34 @@ class AnnotationDriver implements Driver
if ($columnAnnot->type == null) {
throw DoctrineException::propertyTypeIsRequired($property->getName());
}
$mapping['type'] = $columnAnnot->type;
$mapping['length'] = $columnAnnot->length;
$mapping['precision'] = $columnAnnot->precision;
$mapping['scale'] = $columnAnnot->scale;
$mapping['nullable'] = $columnAnnot->nullable;
$mapping['options'] = $columnAnnot->options;
if (isset($columnAnnot->default)) {
$mapping['default'] = $columnAnnot->default;
}
if (isset($columnAnnot->name)) {
$mapping['columnName'] = $columnAnnot->name;
}
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 ($versionAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\Version')) {
$metadata->setVersionMapping($mapping);
}
$metadata->mapField($mapping);
// Check for SequenceGenerator/TableGenerator definition
@ -207,6 +221,7 @@ class AnnotationDriver implements Driver
$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,
@ -249,24 +264,31 @@ class AnnotationDriver implements Driver
foreach ($class->getMethods() as $method) {
if ($method->isPublic()) {
$annotations = $this->_reader->getMethodAnnotations($method);
if (isset($annotations['Doctrine\ORM\Mapping\PrePersist'])) {
$metadata->addLifecycleCallback($method->getName(), \Doctrine\ORM\Events::prePersist);
}
if (isset($annotations['Doctrine\ORM\Mapping\PostPersist'])) {
$metadata->addLifecycleCallback($method->getName(), \Doctrine\ORM\Events::postPersist);
}
if (isset($annotations['Doctrine\ORM\Mapping\PreUpdate'])) {
$metadata->addLifecycleCallback($method->getName(), \Doctrine\ORM\Events::preUpdate);
}
if (isset($annotations['Doctrine\ORM\Mapping\PostUpdate'])) {
$metadata->addLifecycleCallback($method->getName(), \Doctrine\ORM\Events::postUpdate);
}
if (isset($annotations['Doctrine\ORM\Mapping\PreRemove'])) {
$metadata->addLifecycleCallback($method->getName(), \Doctrine\ORM\Events::preRemove);
}
if (isset($annotations['Doctrine\ORM\Mapping\PostRemove'])) {
$metadata->addLifecycleCallback($method->getName(), \Doctrine\ORM\Events::postRemove);
}
if (isset($annotations['Doctrine\ORM\Mapping\PostLoad'])) {
$metadata->addLifecycleCallback($method->getName(), \Doctrine\ORM\Events::postLoad);
}
@ -286,8 +308,9 @@ class AnnotationDriver implements Driver
public function isTransient($className)
{
$classAnnotations = $this->_reader->getClassAnnotations(new \ReflectionClass($className));
return ! isset($classAnnotations['Doctrine\ORM\Mapping\Entity']) &&
! isset($classAnnotations['Doctrine\ORM\Mapping\MappedSuperclass']);
! isset($classAnnotations['Doctrine\ORM\Mapping\MappedSuperclass']);
}
public function preload()

View File

@ -56,10 +56,13 @@ final class JoinColumns extends Annotation {}
final class Column extends Annotation {
public $type;
public $length;
public $precision = 0; // The precision for a decimal (exact numeric) column (Applies only for decimal column)
public $scale = 0; // The scale for a decimal (exact numeric) column (Applies only for decimal column)
public $unique = false;
public $nullable = false;
public $default;
public $name;
public $options = array();
}
final class OneToOne extends Annotation {
public $targetEntity;

View File

@ -59,9 +59,11 @@ class XmlDriver extends AbstractFileDriver
if (isset($xmlRoot['table'])) {
$metadata->primaryTable['name'] = (string)$xmlRoot['table'];
}
if (isset($xmlRoot['schema'])) {
$metadata->primaryTable['schema'] = (string)$xmlRoot['schema'];
}
if (isset($xmlRoot['inheritance-type'])) {
$metadata->setInheritanceType((string)$xmlRoot['inheritance-type']);
}
@ -89,8 +91,9 @@ class XmlDriver extends AbstractFileDriver
// Evaluate <indexes...>
if (isset($xmlRoot->indexes)) {
foreach ($xmlRoot->indexes->index as $index) {
$metadata->primaryTable['indexes'][$index['name']] = array('fields' =>
explode(',', $index['columns']));
$metadata->primaryTable['indexes'][$index['name']] = array(
'fields' => explode(',', $index['columns'])
);
}
}
@ -108,21 +111,27 @@ class XmlDriver extends AbstractFileDriver
'fieldName' => (string)$fieldMapping['name'],
'type' => (string)$fieldMapping['type']
);
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);
}
}
@ -134,9 +143,11 @@ class XmlDriver extends AbstractFileDriver
'fieldName' => (string)$idElement['name'],
'type' => (string)$idElement['type']
);
if (isset($idElement['column'])) {
$mapping['columnName'] = (string)$idElement['column'];
}
$metadata->mapField($mapping);
if (isset($idElement->generator)) {
@ -164,10 +175,12 @@ class XmlDriver extends AbstractFileDriver
'fieldName' => (string)$oneToOneElement['field'],
'targetEntity' => (string)$oneToOneElement['target-entity'],
);
if (isset($oneToOneElement['mapped-by'])) {
$mapping['mappedBy'] = (string)$oneToOneElement['mapped-by'];
} else {
$joinColumns = array();
if (isset($oneToOneElement->{'join-column'})) {
$joinColumns[] = $this->_getJoinColumnMapping($oneToOneElement->{'join-column'});
} else if (isset($oneToOneElement->{'join-columns'})) {
@ -177,14 +190,18 @@ class XmlDriver extends AbstractFileDriver
} else {
throw MappingException::invalidMapping($mapping['fieldName']);
}
$mapping['joinColumns'] = $joinColumns;
}
if (isset($oneToOneElement->cascade)) {
$mapping['cascade'] = $this->_getCascadeMappings($oneToOneElement->cascade);
}
if (isset($oneToOneElement->{'orphan-removal'})) {
$mapping['orphanRemoval'] = (bool)$oneToOneElement->{'orphan-removal'};
}
$metadata->mapOneToOne($mapping);
}
}
@ -197,12 +214,15 @@ class XmlDriver extends AbstractFileDriver
'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);
}
}
@ -214,7 +234,9 @@ class XmlDriver extends AbstractFileDriver
'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'})) {
@ -222,18 +244,23 @@ class XmlDriver extends AbstractFileDriver
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);
}
}
@ -245,6 +272,7 @@ class XmlDriver extends AbstractFileDriver
'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'})) {
@ -252,25 +280,32 @@ class XmlDriver extends AbstractFileDriver
$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']);
}
if (isset($manyToManyElement->cascade)) {
$mapping['cascade'] = $this->_getCascadeMappings($manyToManyElement->cascade);
}
if (isset($manyToManyElement->{'orphan-removal'})) {
$mapping['orphanRemoval'] = (bool)$manyToManyElement->{'orphan-removal'};
}
$metadata->mapManyToMany($mapping);
}
}
@ -279,6 +314,7 @@ class XmlDriver extends AbstractFileDriver
if (isset($xmlRoot->{'lifecycle-callbacks'})) {
foreach ($xmlRoot->{'lifecycle-callbacks'}->{'lifecycle-callback'} as $lifecycleCallback) {
$method = $class->getMethod((string)$lifecycleCallback['method']);
if ($method->isPublic()) {
$metadata->addLifecycleCallback($method->getName(), constant('\Doctrine\ORM\Events::' . (string)$lifecycleCallback['type']));
}
@ -326,15 +362,19 @@ class XmlDriver extends AbstractFileDriver
'name' => (string)$joinColumnElement['name'],
'referencedColumnName' => (string)$joinColumnElement['referenced-column-name']
);
if (isset($joinColumnElement['unique'])) {
$joinColumn['unique'] = (bool)$joinColumnElement['unique'];
}
if (isset($joinColumnElement['nullable'])) {
$joinColumn['nullable'] = (bool)$joinColumnElement['nullable'];
}
if (isset($joinColumnElement['onDelete'])) {
$joinColumn['onDelete'] = (string)$joinColumnElement['on-delete'];
}
if (isset($joinColumnElement['onUpdate'])) {
$joinColumn['onUpdate'] = (string)$joinColumnElement['on-update'];
}
@ -351,15 +391,19 @@ class XmlDriver extends AbstractFileDriver
private function _getCascadeMappings($cascadeElement)
{
$cascades = array();
if (isset($cascadeElement->{'cascade-persist'})) {
$cascades[] = 'persist';
}
if (isset($cascadeElement->{'cascade-remove'})) {
$cascades[] = 'remove';
}
if (isset($cascadeElement->{'cascade-merge'})) {
$cascades[] = 'merge';
}
if (isset($cascadeElement->{'cascade-refresh'})) {
$cascades[] = 'refresh';
}

View File

@ -60,9 +60,11 @@ class YamlDriver extends AbstractFileDriver
if (isset($element['table'])) {
$metadata->primaryTable['name'] = $element['table'];
}
if (isset($element['schema'])) {
$metadata->primaryTable['schema'] = $element['schema'];
}
if (isset($element['inheritanceType'])) {
$metadata->setInheritanceType($element['inheritanceType']);
}
@ -90,8 +92,9 @@ class YamlDriver extends AbstractFileDriver
// Evaluate indexes
if (isset($element['indexes'])) {
foreach ($element['indexes'] as $index) {
$metadata->primaryTable['indexes'][$index['name']] = array('fields' =>
explode(',', $index['columns']));
$metadata->primaryTable['indexes'][$index['name']] = array(
'fields' => explode(',', $index['columns'])
);
}
}
@ -109,21 +112,31 @@ class YamlDriver extends AbstractFileDriver
'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['options'])) {
$mapping['options'] = $fieldMapping['options'];
}
if (isset($fieldMapping['version']) && $fieldMapping['version']) {
$metadata->setVersionMapping($mapping);
}
$metadata->mapField($mapping);
}
}
@ -135,9 +148,11 @@ class YamlDriver extends AbstractFileDriver
'fieldName' => $name,
'type' => $idElement['type']
);
if (isset($idElement['column'])) {
$mapping['columnName'] = $idElement['column'];
}
$metadata->mapField($mapping);
if (isset($idElement['generator'])) {
@ -153,10 +168,12 @@ class YamlDriver extends AbstractFileDriver
'fieldName' => $name,
'targetEntity' => $oneToOneElement['targetEntity']
);
if (isset($oneToOneElement['mappedBy'])) {
$mapping['mappedBy'] = $oneToOneElement['mappedBy'];
} else {
$joinColumns = array();
if (isset($oneToOneElement['joinColumn'])) {
$joinColumns[] = $this->_getJoinColumnMapping($oneToOneElement['joinColumn']);
} else if (isset($oneToOneElement['joinColumns'])) {
@ -164,11 +181,13 @@ class YamlDriver extends AbstractFileDriver
if (!isset($joinColumnElement['name'])) {
$joinColumnElement['name'] = $name;
}
$joinColumns[] = $this->_getJoinColumnMapping($joinColumnElement);
}
} else {
throw MappingException::invalidMapping($mapping['fieldName']);
}
$mapping['joinColumns'] = $joinColumns;
}
@ -188,9 +207,11 @@ class YamlDriver extends AbstractFileDriver
'targetEntity' => $oneToManyElement['targetEntity'],
'mappedBy' => $oneToManyElement['mappedBy']
);
if (isset($oneToManyElement['cascade'])) {
$mapping['cascade'] = $this->_getCascadeMappings($oneToManyElement['cascade']);
}
$metadata->mapOneToMany($mapping);
}
}
@ -202,7 +223,9 @@ class YamlDriver extends AbstractFileDriver
'fieldName' => $name,
'targetEntity' => $manyToOneElement['targetEntity']
);
$joinColumns = array();
if (isset($manyToOneElement['joinColumn'])) {
$joinColumns[] = $this->_getJoinColumnMapping($manyToOneElement['joinColumn']);
} else if (isset($manyToOneElement['joinColumns'])) {
@ -210,15 +233,19 @@ class YamlDriver extends AbstractFileDriver
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);
}
}
@ -238,21 +265,27 @@ class YamlDriver extends AbstractFileDriver
$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']);
@ -270,6 +303,7 @@ class YamlDriver extends AbstractFileDriver
if (isset($element['lifecycleCallbacks'])) {
foreach ($element['lifecycleCallbacks'] as $method => $type) {
$method = $class->getMethod($method);
if ($method->isPublic()) {
$metadata->addLifecycleCallback($method->getName(), constant('\Doctrine\ORM\Events::' . $type));
}
@ -290,15 +324,19 @@ class YamlDriver extends AbstractFileDriver
'name' => $joinColumnElement['name'],
'referencedColumnName' => $joinColumnElement['referencedColumnName']
);
if (isset($joinColumnElement['unique'])) {
$joinColumn['unique'] = (bool) $joinColumnElement['unique'];
}
if (isset($joinColumnElement['nullable'])) {
$joinColumn['nullable'] = (bool) $joinColumnElement['nullable'];
}
if (isset($joinColumnElement['onDelete'])) {
$joinColumn['onDelete'] = $joinColumnElement['onDelete'];
}
if (isset($joinColumnElement['onUpdate'])) {
$joinColumn['onUpdate'] = $joinColumnElement['onUpdate'];
}
@ -315,15 +353,19 @@ class YamlDriver extends AbstractFileDriver
private function _getCascadeMappings($cascadeElement)
{
$cascades = array();
if (isset($cascadeElement['cascadePersist'])) {
$cascades[] = 'persist';
}
if (isset($cascadeElement['cascadeRemove'])) {
$cascades[] = 'remove';
}
if (isset($cascadeElement['cascadeMerge'])) {
$cascades[] = 'merge';
}
if (isset($cascadeElement['cascadeRefresh'])) {
$cascades[] = 'refresh';
}