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
'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()
......
......@@ -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;
......
......@@ -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';
}
......
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