Skip to content

Commit

Permalink
Merge pull request doctrine#5590 from zeroedin-bill/fix-discriminator…
Browse files Browse the repository at this point in the history
…-column-defaults

Fix discriminator column defaults
  • Loading branch information
Ocramius committed Jan 5, 2016
2 parents 7f16e5b + 8bde0c8 commit ebe11c7
Show file tree
Hide file tree
Showing 9 changed files with 183 additions and 8 deletions.
5 changes: 4 additions & 1 deletion docs/en/reference/annotations-reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -236,11 +236,14 @@ Example:
@DiscriminatorColumn
~~~~~~~~~~~~~~~~~~~~~

This annotation is a required annotation for the topmost/super
This annotation is an optional annotation for the topmost/super
class of an inheritance hierarchy. It specifies the details of the
column which saves the name of the class, which the entity is
actually instantiated as.

If this annotation is not specified, the discriminator column defaults
to a string column of length 255 called ``dtype``.

Required attributes:


Expand Down
4 changes: 2 additions & 2 deletions lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,8 @@ public function loadMetadataForClass($className, ClassMetadata $metadata)

$metadata->setDiscriminatorColumn(array(
'name' => $discrColumnAnnot->name,
'type' => $discrColumnAnnot->type,
'length' => $discrColumnAnnot->length,
'type' => $discrColumnAnnot->type ?: 'string',
'length' => $discrColumnAnnot->length ?: 255,
'columnDefinition' => $discrColumnAnnot->columnDefinition,
));
} else {
Expand Down
4 changes: 2 additions & 2 deletions lib/Doctrine/ORM/Mapping/Driver/XmlDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,8 @@ public function loadMetadataForClass($className, ClassMetadata $metadata)
$discrColumn = $xmlRoot->{'discriminator-column'};
$metadata->setDiscriminatorColumn(array(
'name' => isset($discrColumn['name']) ? (string) $discrColumn['name'] : null,
'type' => isset($discrColumn['type']) ? (string) $discrColumn['type'] : null,
'length' => isset($discrColumn['length']) ? (string) $discrColumn['length'] : null,
'type' => isset($discrColumn['type']) ? (string) $discrColumn['type'] : 'string',
'length' => isset($discrColumn['length']) ? (string) $discrColumn['length'] : 255,
'columnDefinition' => isset($discrColumn['column-definition']) ? (string) $discrColumn['column-definition'] : null
));
} else {
Expand Down
4 changes: 2 additions & 2 deletions lib/Doctrine/ORM/Mapping/Driver/YamlDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,8 @@ public function loadMetadataForClass($className, ClassMetadata $metadata)
$discrColumn = $element['discriminatorColumn'];
$metadata->setDiscriminatorColumn(array(
'name' => isset($discrColumn['name']) ? (string) $discrColumn['name'] : null,
'type' => isset($discrColumn['type']) ? (string) $discrColumn['type'] : null,
'length' => isset($discrColumn['length']) ? (string) $discrColumn['length'] : null,
'type' => isset($discrColumn['type']) ? (string) $discrColumn['type'] : 'string',
'length' => isset($discrColumn['length']) ? (string) $discrColumn['length'] : 255,
'columnDefinition' => isset($discrColumn['columnDefinition']) ? (string) $discrColumn['columnDefinition'] : null
));
} else {
Expand Down
114 changes: 113 additions & 1 deletion tests/Doctrine/Tests/ORM/Mapping/AbstractMappingDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Doctrine\ORM\Events;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\ORM\Mapping\DiscriminatorColumn;
use Doctrine\ORM\Mapping\Id;
use Doctrine\Tests\Models\Company\CompanyFixContract;
use Doctrine\Tests\Models\Company\CompanyFlexContract;
use Doctrine\Tests\Models\Cache\City;
Expand Down Expand Up @@ -977,6 +979,52 @@ public function testSchemaDefinitionViaSchemaDefinedInTableNameInTableAnnotation
$this->assertSame('implicit_schema', $metadata->getSchemaName());
$this->assertSame('implicit_table', $metadata->getTableName());
}

/**
* @group DDC-514
* @group DDC-1015
*/
public function testDiscriminatorColumnDefaultLength()
{
if (strpos(get_class($this), 'PHPMappingDriver') !== false) {
$this->markTestSkipped('PHP Mapping Drivers have no defaults.');
}
$class = $this->createClassMetadata(__NAMESPACE__ . '\SingleTableEntityNoDiscriminatorColumnMapping');
$this->assertEquals(255, $class->discriminatorColumn['length']);
$class = $this->createClassMetadata(__NAMESPACE__ . '\SingleTableEntityIncompleteDiscriminatorColumnMapping');
$this->assertEquals(255, $class->discriminatorColumn['length']);
}

/**
* @group DDC-514
* @group DDC-1015
*/
public function testDiscriminatorColumnDefaultType()
{
if (strpos(get_class($this), 'PHPMappingDriver') !== false) {
$this->markTestSkipped('PHP Mapping Drivers have no defaults.');
}
$class = $this->createClassMetadata(__NAMESPACE__ . '\SingleTableEntityNoDiscriminatorColumnMapping');
$this->assertEquals('string', $class->discriminatorColumn['type']);
$class = $this->createClassMetadata(__NAMESPACE__ . '\SingleTableEntityIncompleteDiscriminatorColumnMapping');
$this->assertEquals('string', $class->discriminatorColumn['type']);
}

/**
* @group DDC-514
* @group DDC-1015
*/
public function testDiscriminatorColumnDefaultName()
{
if (strpos(get_class($this), 'PHPMappingDriver') !== false) {
$this->markTestSkipped('PHP Mapping Drivers have no defaults.');
}
$class = $this->createClassMetadata(__NAMESPACE__ . '\SingleTableEntityNoDiscriminatorColumnMapping');
$this->assertEquals('dtype', $class->discriminatorColumn['name']);
$class = $this->createClassMetadata(__NAMESPACE__ . '\SingleTableEntityIncompleteDiscriminatorColumnMapping');
$this->assertEquals('dtype', $class->discriminatorColumn['name']);
}

}

/**
Expand Down Expand Up @@ -1317,7 +1365,6 @@ public static function loadMetadata(ClassMetadataInfo $metadata)
}
}


class DDC807SubClasse1 {}
class DDC807SubClasse2 {}

Expand Down Expand Up @@ -1357,3 +1404,68 @@ public static function loadMetadata(ClassMetadataInfo $metadata)
));
}
}

/**
* @Entity
* @InheritanceType("SINGLE_TABLE")
* @DiscriminatorMap({
* "ONE" = "SingleTableEntityNoDiscriminatorColumnMappingSub1",
* "TWO" = "SingleTableEntityNoDiscriminatorColumnMappingSub2"
* })
*/
class SingleTableEntityNoDiscriminatorColumnMapping
{
/**
* @Id
* @Column(type="integer")
* @GeneratedValue(strategy="NONE")
*/
public $id;

public static function loadMetadata(ClassMetadataInfo $metadata)
{
$metadata->mapField(array(
'id' => true,
'fieldName' => 'id',
));

$metadata->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_NONE);
}
}

class SingleTableEntityNoDiscriminatorColumnMappingSub1 extends SingleTableEntityNoDiscriminatorColumnMapping {}
class SingleTableEntityNoDiscriminatorColumnMappingSub2 extends SingleTableEntityNoDiscriminatorColumnMapping {}

/**
* @Entity
* @InheritanceType("SINGLE_TABLE")
* @DiscriminatorMap({
* "ONE" = "SingleTableEntityIncompleteDiscriminatorColumnMappingSub1",
* "TWO" = "SingleTableEntityIncompleteDiscriminatorColumnMappingSub2"
* })
* @DiscriminatorColumn(name="dtype")
*/
class SingleTableEntityIncompleteDiscriminatorColumnMapping
{
/**
* @Id
* @Column(type="integer")
* @GeneratedValue(strategy="NONE")
*/
public $id;

public static function loadMetadata(ClassMetadataInfo $metadata)
{
$metadata->mapField(array(
'id' => true,
'fieldName' => 'id',
));

$metadata->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_NONE);
}
}

class SingleTableEntityIncompleteDiscriminatorColumnMappingSub1
extends SingleTableEntityIncompleteDiscriminatorColumnMapping {}
class SingleTableEntityIncompleteDiscriminatorColumnMappingSub2
extends SingleTableEntityIncompleteDiscriminatorColumnMapping {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
http://www.doctrine-project.org/schemas/orm/doctrine-mapping.xsd">

<entity name="Doctrine\Tests\ORM\Mapping\SingleTableEntityIncompleteDiscriminatorColumnMapping" inheritance-type="SINGLE_TABLE">
<discriminator-column name="dtype" />

<discriminator-map>
<discriminator-mapping value="ONE" class="SingleTableEntityIncompleteDiscriminatorColumnMappingSub1" />
<discriminator-mapping value="TWO" class="SingleTableEntityIncompleteDiscriminatorColumnMappingSub2" />
</discriminator-map>

<id name="id">
<generator strategy="NONE"/>
</id>
</entity>

</doctrine-mapping>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
http://www.doctrine-project.org/schemas/orm/doctrine-mapping.xsd">

<entity name="Doctrine\Tests\ORM\Mapping\SingleTableEntityNoDiscriminatorColumnMapping" inheritance-type="SINGLE_TABLE">
<discriminator-map>
<discriminator-mapping value="ONE" class="SingleTableEntityNoDiscriminatorColumnMappingSub1" />
<discriminator-mapping value="TWO" class="SingleTableEntityNoDiscriminatorColumnMappingSub2" />
</discriminator-map>

<id name="id">
<generator strategy="NONE"/>
</id>
</entity>

</doctrine-mapping>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Doctrine\Tests\ORM\Mapping\SingleTableEntityIncompleteDiscriminatorColumnMapping:
type: entity
inheritanceType: SINGLE_TABLE
discriminatorMap:
ONE: SingleTableEntityIncompleteDiscriminatorColumnMappingSub1
TWO: SingleTableEntityIncompleteDiscriminatorColumnMappingSub2
discriminatorColumn:
name: dtype
id:
id:
generator:
strategy: NONE
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Doctrine\Tests\ORM\Mapping\SingleTableEntityNoDiscriminatorColumnMapping:
type: entity
inheritanceType: SINGLE_TABLE
discriminatorMap:
ONE: SingleTableEntityNoDiscriminatorColumnMappingSub1
TWO: SingleTableEntityNoDiscriminatorColumnMappingSub2
id:
id:
generator:
strategy: NONE

0 comments on commit ebe11c7

Please sign in to comment.