Skip to content

Commit

Permalink
Allow DiscriminatorColumn with length=0
Browse files Browse the repository at this point in the history
  • Loading branch information
bcremer committed Jan 21, 2022
1 parent d7b7c28 commit 13c6f26
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 4 deletions.
6 changes: 3 additions & 3 deletions lib/Doctrine/ORM/Mapping/DiscriminatorColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
#[Attribute(Attribute::TARGET_CLASS)]
final class DiscriminatorColumn implements Annotation
{
/** @var string */
/** @var string|null */
public $name;

/** @var string */
/** @var string|null */
public $type;

/** @var int */
/** @var int|null */
public $length;

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ public function loadMetadataForClass($className, ClassMetadata $metadata)
[
'name' => $discrColumnAnnot->name,
'type' => $discrColumnAnnot->type ?: 'string',
'length' => $discrColumnAnnot->length ?: 255,
'length' => $discrColumnAnnot->length ?? 255,
'columnDefinition' => $discrColumnAnnot->columnDefinition,
]
);
Expand Down
108 changes: 108 additions & 0 deletions tests/Doctrine/Tests/ORM/Mapping/AnnotationDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Doctrine\ORM\Mapping\GeneratedValue;
use Doctrine\ORM\Mapping\HasLifecycleCallbacks;
use Doctrine\ORM\Mapping\Id;
use Doctrine\ORM\Mapping\DiscriminatorColumn;
use Doctrine\ORM\Mapping\InheritanceType;
use Doctrine\ORM\Mapping\ManyToMany;
use Doctrine\ORM\Mapping\ManyToOne;
Expand Down Expand Up @@ -274,6 +275,29 @@ public function testAttributeOverridesMappingWithTrait(): void
self::assertArrayHasKey('example_trait_bar_id', $metadataWithoutOverride->associationMappings['bar']['joinColumnFieldNames']);
self::assertArrayHasKey('example_entity_overridden_bar_id', $metadataWithOverride->associationMappings['bar']['joinColumnFieldNames']);
}

/**
* @dataProvider provideDiscriminatorColumnTestcases
* @psalm-param class-string $class
*/
public function testLengthForDiscriminatorColumn(string $class, int $expectedLength): void
{
$factory = $this->createClassMetadataFactory();

$metadata = $factory->getMetadataFor($class);

self::assertNotNull($metadata->discriminatorColumn);
self::assertArrayHasKey('length', $metadata->discriminatorColumn);
self::assertSame($expectedLength, $metadata->discriminatorColumn['length']);
}

public function provideDiscriminatorColumnTestcases(): \Generator
{
yield [DiscriminatorColumnWithNullLength::class, 255];
yield [DiscriminatorColumnWithNoLength::class, 255];
yield [DiscriminatorColumnWithZeroLength::class, 0];
yield [DiscriminatorColumnWithNonZeroLength::class, 60];
}
}

/**
Expand Down Expand Up @@ -438,3 +462,87 @@ class AnnotationSLCFoo
*/
public $id;
}

/**
* @Entity
* @InheritanceType("SINGLE_TABLE")
* @DiscriminatorColumn(
* name="type",
* type="string",
* length=0,
* columnDefinition="enum('region','airport','station','poi') NOT NULL",
* ),
* @DiscriminatorMap({"s"="SuperEntity", "c"="ChildEntity"})
*/
class DiscriminatorColumnWithZeroLength
{
/**
* @var int
* @Id
* @Column
*/
public $id;
}

/**
* @Entity
* @InheritanceType("SINGLE_TABLE")
* @DiscriminatorColumn(
* name="type",
* type="string",
* ),
* @DiscriminatorMap({"s"="SuperEntity", "c"="ChildEntity"})
*/
class DiscriminatorColumnWithNoLength
{
/**
* @var int
* @Id
* @Column
*/
public $id;
}

/**
* @Entity
* @InheritanceType("SINGLE_TABLE")
* @DiscriminatorColumn(
* name="type",
* type="string",
* length=60,
* ),
* @DiscriminatorMap({"s"="SuperEntity", "c"="ChildEntity"})
*/
class DiscriminatorColumnWithNonZeroLength
{
/**
* @var int
* @Id
* @Column
*/
public $id;
}

/**
* @Entity
* @InheritanceType("SINGLE_TABLE")
* @DiscriminatorColumn(
* name="type",
* type="string",
* length=null,
* ),
* @DiscriminatorMap({"s"="SuperEntity", "c"="ChildEntity"})
*/
class DiscriminatorColumnWithNullLength
{
/**
* @var int
* @Id
* @Column
*/
public $id;
}

0 comments on commit 13c6f26

Please sign in to comment.