From 5d01123413c9eea68469af1eca37f7ae816ed1cc Mon Sep 17 00:00:00 2001 From: "Fabio B. Silva" Date: Mon, 13 Feb 2012 23:22:49 -0200 Subject: [PATCH] Fix DDC-807, DDC-553 --- doctrine-mapping.xsd | 4 +- .../ORM/Mapping/DiscriminatorColumn.php | 2 + .../ORM/Mapping/Driver/AnnotationDriver.php | 3 +- lib/Doctrine/ORM/Mapping/Driver/XmlDriver.php | 7 +-- .../ORM/Mapping/Driver/YamlDriver.php | 7 +-- lib/Doctrine/ORM/Tools/SchemaTool.php | 13 +++-- .../ORM/Mapping/AbstractMappingDriverTest.php | 51 +++++++++++++++++++ ...octrine.Tests.ORM.Mapping.DDC807Entity.php | 15 ++++++ ...ine.Tests.ORM.Mapping.DDC807Entity.dcm.xml | 20 ++++++++ ...ine.Tests.ORM.Mapping.DDC807Entity.dcm.yml | 13 +++++ 10 files changed, 123 insertions(+), 12 deletions(-) create mode 100644 tests/Doctrine/Tests/ORM/Mapping/php/Doctrine.Tests.ORM.Mapping.DDC807Entity.php create mode 100644 tests/Doctrine/Tests/ORM/Mapping/xml/Doctrine.Tests.ORM.Mapping.DDC807Entity.dcm.xml create mode 100644 tests/Doctrine/Tests/ORM/Mapping/yaml/Doctrine.Tests.ORM.Mapping.DDC807Entity.dcm.yml diff --git a/doctrine-mapping.xsd b/doctrine-mapping.xsd index 227af4403..0d3704a7e 100644 --- a/doctrine-mapping.xsd +++ b/doctrine-mapping.xsd @@ -203,9 +203,10 @@ - + + @@ -278,6 +279,7 @@ + diff --git a/lib/Doctrine/ORM/Mapping/DiscriminatorColumn.php b/lib/Doctrine/ORM/Mapping/DiscriminatorColumn.php index aec011538..0a9d6e9d5 100644 --- a/lib/Doctrine/ORM/Mapping/DiscriminatorColumn.php +++ b/lib/Doctrine/ORM/Mapping/DiscriminatorColumn.php @@ -33,4 +33,6 @@ final class DiscriminatorColumn implements Annotation public $length; /** @var mixed */ public $fieldName; // field name used in non-object hydration (array/scalar) + /** @var string */ + public $columnDefinition; } diff --git a/lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php b/lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php index e95d32e94..93e8473a4 100644 --- a/lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php +++ b/lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php @@ -228,7 +228,8 @@ class AnnotationDriver implements Driver $metadata->setDiscriminatorColumn(array( 'name' => $discrColumnAnnot->name, 'type' => $discrColumnAnnot->type, - 'length' => $discrColumnAnnot->length + 'length' => $discrColumnAnnot->length, + 'columnDefinition' => $discrColumnAnnot->columnDefinition )); } else { $metadata->setDiscriminatorColumn(array('name' => 'dtype', 'type' => 'string', 'length' => 255)); diff --git a/lib/Doctrine/ORM/Mapping/Driver/XmlDriver.php b/lib/Doctrine/ORM/Mapping/Driver/XmlDriver.php index a528346e6..b62df3893 100644 --- a/lib/Doctrine/ORM/Mapping/Driver/XmlDriver.php +++ b/lib/Doctrine/ORM/Mapping/Driver/XmlDriver.php @@ -99,9 +99,10 @@ class XmlDriver extends AbstractFileDriver if (isset($xmlRoot->{'discriminator-column'})) { $discrColumn = $xmlRoot->{'discriminator-column'}; $metadata->setDiscriminatorColumn(array( - 'name' => (string)$discrColumn['name'], - 'type' => (string)$discrColumn['type'], - 'length' => (string)$discrColumn['length'] + '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)); diff --git a/lib/Doctrine/ORM/Mapping/Driver/YamlDriver.php b/lib/Doctrine/ORM/Mapping/Driver/YamlDriver.php index 0106f3aba..ef0eff6ec 100644 --- a/lib/Doctrine/ORM/Mapping/Driver/YamlDriver.php +++ b/lib/Doctrine/ORM/Mapping/Driver/YamlDriver.php @@ -96,9 +96,10 @@ class YamlDriver extends AbstractFileDriver if (isset($element['discriminatorColumn'])) { $discrColumn = $element['discriminatorColumn']; $metadata->setDiscriminatorColumn(array( - 'name' => $discrColumn['name'], - 'type' => $discrColumn['type'], - 'length' => $discrColumn['length'] + '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)); diff --git a/lib/Doctrine/ORM/Tools/SchemaTool.php b/lib/Doctrine/ORM/Tools/SchemaTool.php index 85e50b2b0..027285c27 100644 --- a/lib/Doctrine/ORM/Tools/SchemaTool.php +++ b/lib/Doctrine/ORM/Tools/SchemaTool.php @@ -288,11 +288,16 @@ class SchemaTool $discrColumn['length'] = 255; } - $table->addColumn( - $discrColumn['name'], - $discrColumn['type'], - array('length' => $discrColumn['length'], 'notnull' => true) + $options = array( + 'length' => isset($discrColumn['length']) ? $discrColumn['length'] : null, + 'notnull' => true ); + + if (isset($discrColumn['columnDefinition'])) { + $options['columnDefinition'] = $discrColumn['columnDefinition']; + } + + $table->addColumn($discrColumn['name'], $discrColumn['type'], $options); } /** diff --git a/tests/Doctrine/Tests/ORM/Mapping/AbstractMappingDriverTest.php b/tests/Doctrine/Tests/ORM/Mapping/AbstractMappingDriverTest.php index 369c8fdeb..4b2da0a70 100644 --- a/tests/Doctrine/Tests/ORM/Mapping/AbstractMappingDriverTest.php +++ b/tests/Doctrine/Tests/ORM/Mapping/AbstractMappingDriverTest.php @@ -434,6 +434,21 @@ abstract class AbstractMappingDriverTest extends \Doctrine\Tests\OrmTestCase $this->assertEquals('NAME', $class->columnNames['name']); $this->assertEquals('DDC1476ENTITY_WITH_DEFAULT_FIELD_TYPE', $class->table['name']); } + + /** + * @group DDC-807 + * @group DDC-553 + */ + public function testDiscriminatorColumnDefinition() + { + $class = $this->createClassMetadata(__NAMESPACE__ . '\DDC807Entity'); + + $this->assertArrayHasKey('columnDefinition', $class->discriminatorColumn); + $this->assertArrayHasKey('name', $class->discriminatorColumn); + + $this->assertEquals("ENUM('ONE','TWO')", $class->discriminatorColumn['columnDefinition']); + $this->assertEquals("dtype", $class->discriminatorColumn['name']); + } } /** @@ -724,6 +739,42 @@ class DDC1170Entity } +/** + * @Entity + * @InheritanceType("SINGLE_TABLE") + * @DiscriminatorMap({"ONE" = "DDC807SubClasse1", "TWO" = "DDC807SubClasse2"}) + * @DiscriminatorColumn(name = "dtype", columnDefinition="ENUM('ONE','TWO')") + */ +class DDC807Entity +{ + /** + * @Id + * @Column(type="integer") + * @GeneratedValue(strategy="NONE") + **/ + public $id; + + public static function loadMetadata(ClassMetadataInfo $metadata) + { + $metadata->mapField(array( + 'id' => true, + 'fieldName' => 'id', + )); + + $metadata->setDiscriminatorColumn(array( + 'name' => "dtype", + 'type' => "string", + 'columnDefinition' => "ENUM('ONE','TWO')" + )); + + $metadata->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_NONE); + } +} + + +class DDC807SubClasse1 {} +class DDC807SubClasse2 {} + class Address {} class Phonenumber {} class Group {} diff --git a/tests/Doctrine/Tests/ORM/Mapping/php/Doctrine.Tests.ORM.Mapping.DDC807Entity.php b/tests/Doctrine/Tests/ORM/Mapping/php/Doctrine.Tests.ORM.Mapping.DDC807Entity.php new file mode 100644 index 000000000..1682d7a0d --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Mapping/php/Doctrine.Tests.ORM.Mapping.DDC807Entity.php @@ -0,0 +1,15 @@ +mapField(array( + 'id' => true, + 'fieldName' => 'id', +)); + +$metadata->setDiscriminatorColumn(array( + 'name' => "dtype", + 'columnDefinition' => "ENUM('ONE','TWO')" +)); + +$metadata->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_NONE); \ No newline at end of file diff --git a/tests/Doctrine/Tests/ORM/Mapping/xml/Doctrine.Tests.ORM.Mapping.DDC807Entity.dcm.xml b/tests/Doctrine/Tests/ORM/Mapping/xml/Doctrine.Tests.ORM.Mapping.DDC807Entity.dcm.xml new file mode 100644 index 000000000..3dc9135c5 --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Mapping/xml/Doctrine.Tests.ORM.Mapping.DDC807Entity.dcm.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/tests/Doctrine/Tests/ORM/Mapping/yaml/Doctrine.Tests.ORM.Mapping.DDC807Entity.dcm.yml b/tests/Doctrine/Tests/ORM/Mapping/yaml/Doctrine.Tests.ORM.Mapping.DDC807Entity.dcm.yml new file mode 100644 index 000000000..20db3c328 --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Mapping/yaml/Doctrine.Tests.ORM.Mapping.DDC807Entity.dcm.yml @@ -0,0 +1,13 @@ +Doctrine\Tests\ORM\Mapping\DDC807Entity: + type: entity + inheritanceType: SINGLE_TABLE + discriminatorMap: + ONE: DDC807SubClasse1 + TWO: DDC807SubClasse2 + discriminatorColumn: + name: dtype + columnDefinition: ENUM('ONE','TWO') + id: + id: + generator: + strategy: NONE \ No newline at end of file