Commit eb254226 authored by guilhermeblanco's avatar guilhermeblanco

[2.0] Fixing spacing and adding precision and scale in Annotations driver....

[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.
parent 203b46de
...@@ -77,16 +77,19 @@ class AnnotationDriver implements Driver ...@@ -77,16 +77,19 @@ class AnnotationDriver implements Driver
'name' => $tableAnnot->name, 'name' => $tableAnnot->name,
'schema' => $tableAnnot->schema 'schema' => $tableAnnot->schema
); );
if ($tableAnnot->indexes !== null) { if ($tableAnnot->indexes !== null) {
foreach ($tableAnnot->indexes as $indexAnnot) { foreach ($tableAnnot->indexes as $indexAnnot) {
$primaryTable['indexes'][$indexAnnot->name] = array('fields' => $indexAnnot->columns); $primaryTable['indexes'][$indexAnnot->name] = array('fields' => $indexAnnot->columns);
} }
} }
if ($tableAnnot->uniqueConstraints !== null) { if ($tableAnnot->uniqueConstraints !== null) {
foreach ($tableAnnot->uniqueConstraints as $uniqueConstraint) { foreach ($tableAnnot->uniqueConstraints as $uniqueConstraint) {
$primaryTable['uniqueConstraints'][] = $uniqueConstraint->columns; $primaryTable['uniqueConstraints'][] = $uniqueConstraint->columns;
} }
} }
$metadata->setPrimaryTable($primaryTable); $metadata->setPrimaryTable($primaryTable);
} }
...@@ -129,6 +132,7 @@ class AnnotationDriver implements Driver ...@@ -129,6 +132,7 @@ class AnnotationDriver implements Driver
// Check for JoinColummn/JoinColumns annotations // Check for JoinColummn/JoinColumns annotations
$joinColumns = array(); $joinColumns = array();
if ($joinColumnAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\JoinColumn')) { if ($joinColumnAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\JoinColumn')) {
$joinColumns[] = array( $joinColumns[] = array(
'name' => $joinColumnAnnot->name, 'name' => $joinColumnAnnot->name,
...@@ -157,24 +161,34 @@ class AnnotationDriver implements Driver ...@@ -157,24 +161,34 @@ class AnnotationDriver implements Driver
if ($columnAnnot->type == null) { if ($columnAnnot->type == null) {
throw DoctrineException::propertyTypeIsRequired($property->getName()); throw DoctrineException::propertyTypeIsRequired($property->getName());
} }
$mapping['type'] = $columnAnnot->type; $mapping['type'] = $columnAnnot->type;
$mapping['length'] = $columnAnnot->length; $mapping['length'] = $columnAnnot->length;
$mapping['precision'] = $columnAnnot->precision;
$mapping['scale'] = $columnAnnot->scale;
$mapping['nullable'] = $columnAnnot->nullable; $mapping['nullable'] = $columnAnnot->nullable;
$mapping['options'] = $columnAnnot->options;
if (isset($columnAnnot->default)) { if (isset($columnAnnot->default)) {
$mapping['default'] = $columnAnnot->default; $mapping['default'] = $columnAnnot->default;
} }
if (isset($columnAnnot->name)) { if (isset($columnAnnot->name)) {
$mapping['columnName'] = $columnAnnot->name; $mapping['columnName'] = $columnAnnot->name;
} }
if ($idAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\Id')) { if ($idAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\Id')) {
$mapping['id'] = true; $mapping['id'] = true;
} }
if ($generatedValueAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\GeneratedValue')) { if ($generatedValueAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\GeneratedValue')) {
$metadata->setIdGeneratorType(constant('Doctrine\ORM\Mapping\ClassMetadata::GENERATOR_TYPE_' . $generatedValueAnnot->strategy)); $metadata->setIdGeneratorType(constant('Doctrine\ORM\Mapping\ClassMetadata::GENERATOR_TYPE_' . $generatedValueAnnot->strategy));
} }
if ($versionAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\Version')) { if ($versionAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\Version')) {
$metadata->setVersionMapping($mapping); $metadata->setVersionMapping($mapping);
} }
$metadata->mapField($mapping); $metadata->mapField($mapping);
// Check for SequenceGenerator/TableGenerator definition // Check for SequenceGenerator/TableGenerator definition
...@@ -207,6 +221,7 @@ class AnnotationDriver implements Driver ...@@ -207,6 +221,7 @@ class AnnotationDriver implements Driver
$metadata->mapManyToOne($mapping); $metadata->mapManyToOne($mapping);
} else if ($manyToManyAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\ManyToMany')) { } else if ($manyToManyAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\ManyToMany')) {
$joinTable = array(); $joinTable = array();
if ($joinTableAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\JoinTable')) { if ($joinTableAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\JoinTable')) {
$joinTable = array( $joinTable = array(
'name' => $joinTableAnnot->name, 'name' => $joinTableAnnot->name,
...@@ -249,24 +264,31 @@ class AnnotationDriver implements Driver ...@@ -249,24 +264,31 @@ class AnnotationDriver implements Driver
foreach ($class->getMethods() as $method) { foreach ($class->getMethods() as $method) {
if ($method->isPublic()) { if ($method->isPublic()) {
$annotations = $this->_reader->getMethodAnnotations($method); $annotations = $this->_reader->getMethodAnnotations($method);
if (isset($annotations['Doctrine\ORM\Mapping\PrePersist'])) { if (isset($annotations['Doctrine\ORM\Mapping\PrePersist'])) {
$metadata->addLifecycleCallback($method->getName(), \Doctrine\ORM\Events::prePersist); $metadata->addLifecycleCallback($method->getName(), \Doctrine\ORM\Events::prePersist);
} }
if (isset($annotations['Doctrine\ORM\Mapping\PostPersist'])) { if (isset($annotations['Doctrine\ORM\Mapping\PostPersist'])) {
$metadata->addLifecycleCallback($method->getName(), \Doctrine\ORM\Events::postPersist); $metadata->addLifecycleCallback($method->getName(), \Doctrine\ORM\Events::postPersist);
} }
if (isset($annotations['Doctrine\ORM\Mapping\PreUpdate'])) { if (isset($annotations['Doctrine\ORM\Mapping\PreUpdate'])) {
$metadata->addLifecycleCallback($method->getName(), \Doctrine\ORM\Events::preUpdate); $metadata->addLifecycleCallback($method->getName(), \Doctrine\ORM\Events::preUpdate);
} }
if (isset($annotations['Doctrine\ORM\Mapping\PostUpdate'])) { if (isset($annotations['Doctrine\ORM\Mapping\PostUpdate'])) {
$metadata->addLifecycleCallback($method->getName(), \Doctrine\ORM\Events::postUpdate); $metadata->addLifecycleCallback($method->getName(), \Doctrine\ORM\Events::postUpdate);
} }
if (isset($annotations['Doctrine\ORM\Mapping\PreRemove'])) { if (isset($annotations['Doctrine\ORM\Mapping\PreRemove'])) {
$metadata->addLifecycleCallback($method->getName(), \Doctrine\ORM\Events::preRemove); $metadata->addLifecycleCallback($method->getName(), \Doctrine\ORM\Events::preRemove);
} }
if (isset($annotations['Doctrine\ORM\Mapping\PostRemove'])) { if (isset($annotations['Doctrine\ORM\Mapping\PostRemove'])) {
$metadata->addLifecycleCallback($method->getName(), \Doctrine\ORM\Events::postRemove); $metadata->addLifecycleCallback($method->getName(), \Doctrine\ORM\Events::postRemove);
} }
if (isset($annotations['Doctrine\ORM\Mapping\PostLoad'])) { if (isset($annotations['Doctrine\ORM\Mapping\PostLoad'])) {
$metadata->addLifecycleCallback($method->getName(), \Doctrine\ORM\Events::postLoad); $metadata->addLifecycleCallback($method->getName(), \Doctrine\ORM\Events::postLoad);
} }
...@@ -286,8 +308,9 @@ class AnnotationDriver implements Driver ...@@ -286,8 +308,9 @@ class AnnotationDriver implements Driver
public function isTransient($className) public function isTransient($className)
{ {
$classAnnotations = $this->_reader->getClassAnnotations(new \ReflectionClass($className)); $classAnnotations = $this->_reader->getClassAnnotations(new \ReflectionClass($className));
return ! isset($classAnnotations['Doctrine\ORM\Mapping\Entity']) && return ! isset($classAnnotations['Doctrine\ORM\Mapping\Entity']) &&
! isset($classAnnotations['Doctrine\ORM\Mapping\MappedSuperclass']); ! isset($classAnnotations['Doctrine\ORM\Mapping\MappedSuperclass']);
} }
public function preload() public function preload()
......
...@@ -56,10 +56,13 @@ final class JoinColumns extends Annotation {} ...@@ -56,10 +56,13 @@ final class JoinColumns extends Annotation {}
final class Column extends Annotation { final class Column extends Annotation {
public $type; public $type;
public $length; 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 $unique = false;
public $nullable = false; public $nullable = false;
public $default; public $default;
public $name; public $name;
public $options = array();
} }
final class OneToOne extends Annotation { final class OneToOne extends Annotation {
public $targetEntity; public $targetEntity;
......
...@@ -60,9 +60,11 @@ class YamlDriver extends AbstractFileDriver ...@@ -60,9 +60,11 @@ class YamlDriver extends AbstractFileDriver
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']);
} }
...@@ -90,8 +92,9 @@ class YamlDriver extends AbstractFileDriver ...@@ -90,8 +92,9 @@ class YamlDriver extends AbstractFileDriver
// Evaluate indexes // Evaluate indexes
if (isset($element['indexes'])) { if (isset($element['indexes'])) {
foreach ($element['indexes'] as $index) { foreach ($element['indexes'] as $index) {
$metadata->primaryTable['indexes'][$index['name']] = array('fields' => $metadata->primaryTable['indexes'][$index['name']] = array(
explode(',', $index['columns'])); 'fields' => explode(',', $index['columns'])
);
} }
} }
...@@ -109,21 +112,31 @@ class YamlDriver extends AbstractFileDriver ...@@ -109,21 +112,31 @@ class YamlDriver extends AbstractFileDriver
'fieldName' => $name, 'fieldName' => $name,
'type' => $fieldMapping['type'] 'type' => $fieldMapping['type']
); );
if (isset($fieldMapping['column'])) { if (isset($fieldMapping['column'])) {
$mapping['columnName'] = $fieldMapping['column']; $mapping['columnName'] = $fieldMapping['column'];
} }
if (isset($fieldMapping['length'])) { if (isset($fieldMapping['length'])) {
$mapping['length'] = $fieldMapping['length']; $mapping['length'] = $fieldMapping['length'];
} }
if (isset($fieldMapping['precision'])) { if (isset($fieldMapping['precision'])) {
$mapping['precision'] = $fieldMapping['precision']; $mapping['precision'] = $fieldMapping['precision'];
} }
if (isset($fieldMapping['scale'])) { if (isset($fieldMapping['scale'])) {
$mapping['scale'] = $fieldMapping['scale']; $mapping['scale'] = $fieldMapping['scale'];
} }
if (isset($fieldMapping['options'])) {
$mapping['options'] = $fieldMapping['options'];
}
if (isset($fieldMapping['version']) && $fieldMapping['version']) { if (isset($fieldMapping['version']) && $fieldMapping['version']) {
$metadata->setVersionMapping($mapping); $metadata->setVersionMapping($mapping);
} }
$metadata->mapField($mapping); $metadata->mapField($mapping);
} }
} }
...@@ -135,9 +148,11 @@ class YamlDriver extends AbstractFileDriver ...@@ -135,9 +148,11 @@ class YamlDriver extends AbstractFileDriver
'fieldName' => $name, 'fieldName' => $name,
'type' => $idElement['type'] 'type' => $idElement['type']
); );
if (isset($idElement['column'])) { if (isset($idElement['column'])) {
$mapping['columnName'] = $idElement['column']; $mapping['columnName'] = $idElement['column'];
} }
$metadata->mapField($mapping); $metadata->mapField($mapping);
if (isset($idElement['generator'])) { if (isset($idElement['generator'])) {
...@@ -153,10 +168,12 @@ class YamlDriver extends AbstractFileDriver ...@@ -153,10 +168,12 @@ class YamlDriver extends AbstractFileDriver
'fieldName' => $name, 'fieldName' => $name,
'targetEntity' => $oneToOneElement['targetEntity'] 'targetEntity' => $oneToOneElement['targetEntity']
); );
if (isset($oneToOneElement['mappedBy'])) { if (isset($oneToOneElement['mappedBy'])) {
$mapping['mappedBy'] = $oneToOneElement['mappedBy']; $mapping['mappedBy'] = $oneToOneElement['mappedBy'];
} else { } else {
$joinColumns = array(); $joinColumns = array();
if (isset($oneToOneElement['joinColumn'])) { if (isset($oneToOneElement['joinColumn'])) {
$joinColumns[] = $this->_getJoinColumnMapping($oneToOneElement['joinColumn']); $joinColumns[] = $this->_getJoinColumnMapping($oneToOneElement['joinColumn']);
} else if (isset($oneToOneElement['joinColumns'])) { } else if (isset($oneToOneElement['joinColumns'])) {
...@@ -164,11 +181,13 @@ class YamlDriver extends AbstractFileDriver ...@@ -164,11 +181,13 @@ class YamlDriver extends AbstractFileDriver
if (!isset($joinColumnElement['name'])) { if (!isset($joinColumnElement['name'])) {
$joinColumnElement['name'] = $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;
} }
...@@ -188,9 +207,11 @@ class YamlDriver extends AbstractFileDriver ...@@ -188,9 +207,11 @@ class YamlDriver extends AbstractFileDriver
'targetEntity' => $oneToManyElement['targetEntity'], 'targetEntity' => $oneToManyElement['targetEntity'],
'mappedBy' => $oneToManyElement['mappedBy'] 'mappedBy' => $oneToManyElement['mappedBy']
); );
if (isset($oneToManyElement['cascade'])) { if (isset($oneToManyElement['cascade'])) {
$mapping['cascade'] = $this->_getCascadeMappings($oneToManyElement['cascade']); $mapping['cascade'] = $this->_getCascadeMappings($oneToManyElement['cascade']);
} }
$metadata->mapOneToMany($mapping); $metadata->mapOneToMany($mapping);
} }
} }
...@@ -202,7 +223,9 @@ class YamlDriver extends AbstractFileDriver ...@@ -202,7 +223,9 @@ class YamlDriver extends AbstractFileDriver
'fieldName' => $name, 'fieldName' => $name,
'targetEntity' => $manyToOneElement['targetEntity'] 'targetEntity' => $manyToOneElement['targetEntity']
); );
$joinColumns = array(); $joinColumns = array();
if (isset($manyToOneElement['joinColumn'])) { if (isset($manyToOneElement['joinColumn'])) {
$joinColumns[] = $this->_getJoinColumnMapping($manyToOneElement['joinColumn']); $joinColumns[] = $this->_getJoinColumnMapping($manyToOneElement['joinColumn']);
} else if (isset($manyToOneElement['joinColumns'])) { } else if (isset($manyToOneElement['joinColumns'])) {
...@@ -210,15 +233,19 @@ class YamlDriver extends AbstractFileDriver ...@@ -210,15 +233,19 @@ class YamlDriver extends AbstractFileDriver
if (!isset($joinColumnElement['name'])) { if (!isset($joinColumnElement['name'])) {
$joinColumnElement['name'] = $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'])) { if (isset($manyToOneElement['cascade'])) {
$mapping['cascade'] = $this->_getCascadeMappings($manyToOneElement['cascade']); $mapping['cascade'] = $this->_getCascadeMappings($manyToOneElement['cascade']);
} }
$metadata->mapManyToOne($mapping); $metadata->mapManyToOne($mapping);
} }
} }
...@@ -238,21 +265,27 @@ class YamlDriver extends AbstractFileDriver ...@@ -238,21 +265,27 @@ class YamlDriver extends AbstractFileDriver
$joinTable = array( $joinTable = array(
'name' => $joinTableElement['name'] 'name' => $joinTableElement['name']
); );
if (isset($joinTableElement['schema'])) { if (isset($joinTableElement['schema'])) {
$joinTable['schema'] = $joinTableElement['schema']; $joinTable['schema'] = $joinTableElement['schema'];
} }
foreach ($joinTableElement['joinColumns'] as $name => $joinColumnElement) { foreach ($joinTableElement['joinColumns'] as $name => $joinColumnElement) {
if (!isset($joinColumnElement['name'])) { if (!isset($joinColumnElement['name'])) {
$joinColumnElement['name'] = $name; $joinColumnElement['name'] = $name;
} }
$joinTable['joinColumns'][] = $this->_getJoinColumnMapping($joinColumnElement); $joinTable['joinColumns'][] = $this->_getJoinColumnMapping($joinColumnElement);
} }
foreach ($joinTableElement['inverseJoinColumns'] as $name => $joinColumnElement) { foreach ($joinTableElement['inverseJoinColumns'] as $name => $joinColumnElement) {
if (!isset($joinColumnElement['name'])) { if (!isset($joinColumnElement['name'])) {
$joinColumnElement['name'] = $name; $joinColumnElement['name'] = $name;
} }
$joinTable['inverseJoinColumns'][] = $this->_getJoinColumnMapping($joinColumnElement); $joinTable['inverseJoinColumns'][] = $this->_getJoinColumnMapping($joinColumnElement);
} }
$mapping['joinTable'] = $joinTable; $mapping['joinTable'] = $joinTable;
} else { } else {
throw MappingException::invalidMapping($mapping['fieldName']); throw MappingException::invalidMapping($mapping['fieldName']);
...@@ -270,6 +303,7 @@ class YamlDriver extends AbstractFileDriver ...@@ -270,6 +303,7 @@ class YamlDriver extends AbstractFileDriver
if (isset($element['lifecycleCallbacks'])) { if (isset($element['lifecycleCallbacks'])) {
foreach ($element['lifecycleCallbacks'] as $method => $type) { foreach ($element['lifecycleCallbacks'] as $method => $type) {
$method = $class->getMethod($method); $method = $class->getMethod($method);
if ($method->isPublic()) { if ($method->isPublic()) {
$metadata->addLifecycleCallback($method->getName(), constant('\Doctrine\ORM\Events::' . $type)); $metadata->addLifecycleCallback($method->getName(), constant('\Doctrine\ORM\Events::' . $type));
} }
...@@ -290,15 +324,19 @@ class YamlDriver extends AbstractFileDriver ...@@ -290,15 +324,19 @@ class YamlDriver extends AbstractFileDriver
'name' => $joinColumnElement['name'], 'name' => $joinColumnElement['name'],
'referencedColumnName' => $joinColumnElement['referencedColumnName'] 'referencedColumnName' => $joinColumnElement['referencedColumnName']
); );
if (isset($joinColumnElement['unique'])) { if (isset($joinColumnElement['unique'])) {
$joinColumn['unique'] = (bool) $joinColumnElement['unique']; $joinColumn['unique'] = (bool) $joinColumnElement['unique'];
} }
if (isset($joinColumnElement['nullable'])) { if (isset($joinColumnElement['nullable'])) {
$joinColumn['nullable'] = (bool) $joinColumnElement['nullable']; $joinColumn['nullable'] = (bool) $joinColumnElement['nullable'];
} }
if (isset($joinColumnElement['onDelete'])) { if (isset($joinColumnElement['onDelete'])) {
$joinColumn['onDelete'] = $joinColumnElement['onDelete']; $joinColumn['onDelete'] = $joinColumnElement['onDelete'];
} }
if (isset($joinColumnElement['onUpdate'])) { if (isset($joinColumnElement['onUpdate'])) {
$joinColumn['onUpdate'] = $joinColumnElement['onUpdate']; $joinColumn['onUpdate'] = $joinColumnElement['onUpdate'];
} }
...@@ -315,15 +353,19 @@ class YamlDriver extends AbstractFileDriver ...@@ -315,15 +353,19 @@ class YamlDriver extends AbstractFileDriver
private function _getCascadeMappings($cascadeElement) private function _getCascadeMappings($cascadeElement)
{ {
$cascades = array(); $cascades = array();
if (isset($cascadeElement['cascadePersist'])) { if (isset($cascadeElement['cascadePersist'])) {
$cascades[] = 'persist'; $cascades[] = 'persist';
} }
if (isset($cascadeElement['cascadeRemove'])) { if (isset($cascadeElement['cascadeRemove'])) {
$cascades[] = 'remove'; $cascades[] = 'remove';
} }
if (isset($cascadeElement['cascadeMerge'])) { if (isset($cascadeElement['cascadeMerge'])) {
$cascades[] = 'merge'; $cascades[] = 'merge';
} }
if (isset($cascadeElement['cascadeRefresh'])) { if (isset($cascadeElement['cascadeRefresh'])) {
$cascades[] = 'refresh'; $cascades[] = 'refresh';
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment