Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow DiscriminatorColumn with length=0 #9410

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
106 changes: 106 additions & 0 deletions tests/Doctrine/Tests/ORM/Mapping/AnnotationDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Mapping\ClassMetadataFactory;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\DiscriminatorColumn;
use Doctrine\ORM\Mapping\DiscriminatorMap;
use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
use Doctrine\ORM\Mapping\Entity;
Expand All @@ -33,6 +34,7 @@
use Doctrine\Tests\Models\DirectoryTree\Directory;
use Doctrine\Tests\Models\DirectoryTree\File;
use Doctrine\Tests\Models\ECommerce\ECommerceCart;
use Generator;

class AnnotationDriverTest extends AbstractMappingDriverTest
{
Expand Down Expand Up @@ -274,6 +276,30 @@ 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']);
}

/**
* @psalm-param class-string $class
*
* @dataProvider provideDiscriminatorColumnTestcases
*/
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 +464,83 @@ 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;
}