[PR #9410] Allow DiscriminatorColumn with length=0 #11592

Closed
opened 2026-01-22 16:11:15 +01:00 by admin · 0 comments
Owner

Original Pull Request: https://github.com/doctrine/orm/pull/9410

State: closed
Merged: Yes


This patch allows to set length to a Zero-Value on a @ORM\DiscriminatorColumn:

/**
 * @ORM\DiscriminatorColumn(
 *     name="type",
 *     type="string",
 *     length=0
 * )
 */
abstract class MyEntityClass

Right now a value <1 will be replaced by the default value (255) of a ternary operation in \Doctrine\ORM\Mapping\Driver\AnnotationDriver::loadMetadataForClass .
This seems to be an accidental results from the weak type comparison.

On a @ORM\Column a length of Zero will be parsed as is and not changed to 255.

    /**
     * @ORM\Column (
     *     name="example", 
     *     type="string", 
     *     length=0, 
     *     nullable=true
     * )
     */
    private string $example;

Background:

I'm mapping MySQL enums to strings as described here: https://www.doctrine-project.org/projects/doctrine-orm/en/2.7/cookbook/mysql-enums.html#solution-1-mapping-to-varchars

Since the 3.2 release with the new Platform-aware schema comparison (
https://www.doctrine-project.org/2021/11/26/dbal-3.2.0.html) I get a lot of diffs in our enum fields.

    /**
     * @ORM\Column (
     *   name="platform",
     *   type="string",
     *   columnDefinition="enum('iOS','Android') NULL DEFAULT",
     */
    private string $platform;

The schema diff will report different values for the length-property as the MySQl Platform yields lenght = 0 for the mysql native ENUM type.

To fix the diff the length has to be specified as 0:

    /**
     * @ORM\Column (
     *   name="platform",
     *   type="string",
     *   length=0,
     *   columnDefinition="enum('iOS','Android') NULL DEFAULT",
     */
    private string $platform;

This works for all columns and the enum fields won't generate diffs anymore.

The same lenght=0 change is required if the DiscriminatorColumn uses a native Enum type like so:

/**
 * @ORM\Table
 * @ORM\Entity
 * @ORM\InheritanceType("SINGLE_TABLE")
 * @ORM\DiscriminatorColumn(
 *     name="type",
 *     type="string",
 *     length=0,
 *     columnDefinition="enum('region','airport','station','poi') NOT NULL",
 * )
 * @ORM\DiscriminatorMap({
 *     "region" = "\Storage\MySQL\ApiV3\Entity\Poi\Region",
 *     "airport" = "\Storage\MySQL\ApiV3\Entity\Poi\Airport",
 *     "station" = "\Storage\MySQL\ApiV3\Entity\Poi\Station",
 *     "poi" = "\Storage\MySQL\ApiV3\Entity\Poi\GeneralPoi",
 * })
 */
abstract class Poi

Right now this is not possible as length=0 on a DiscriminatorColumn will become length=255 and thus a diff will be reported from the schema comparison.

**Original Pull Request:** https://github.com/doctrine/orm/pull/9410 **State:** closed **Merged:** Yes --- This patch allows to set `length` to a Zero-Value on a `@ORM\DiscriminatorColumn`: ```php /** * @ORM\DiscriminatorColumn( * name="type", * type="string", * length=0 * ) */ abstract class MyEntityClass ``` Right now a value <1 will be replaced by the default value (255) of a ternary operation in `\Doctrine\ORM\Mapping\Driver\AnnotationDriver::loadMetadataForClass` . This seems to be an accidental results from the weak type comparison. On a `@ORM\Column` a length of Zero will be parsed as is and not changed to 255. ```php /** * @ORM\Column ( * name="example", * type="string", * length=0, * nullable=true * ) */ private string $example; ``` ## Background: I'm mapping MySQL enums to strings as described here: https://www.doctrine-project.org/projects/doctrine-orm/en/2.7/cookbook/mysql-enums.html#solution-1-mapping-to-varchars Since the 3.2 release with the new Platform-aware schema comparison ( https://www.doctrine-project.org/2021/11/26/dbal-3.2.0.html) I get a lot of diffs in our enum fields. ```php /** * @ORM\Column ( * name="platform", * type="string", * columnDefinition="enum('iOS','Android') NULL DEFAULT", */ private string $platform; ``` The schema diff will report different values for the `length`-property as the MySQl Platform yields lenght = 0 for the mysql native ENUM type. To fix the diff the length has to be specified as 0: ```php /** * @ORM\Column ( * name="platform", * type="string", * length=0, * columnDefinition="enum('iOS','Android') NULL DEFAULT", */ private string $platform; ``` This works for all columns and the enum fields won't generate diffs anymore. The same lenght=0 change is required if the DiscriminatorColumn uses a native Enum type like so: ```php /** * @ORM\Table * @ORM\Entity * @ORM\InheritanceType("SINGLE_TABLE") * @ORM\DiscriminatorColumn( * name="type", * type="string", * length=0, * columnDefinition="enum('region','airport','station','poi') NOT NULL", * ) * @ORM\DiscriminatorMap({ * "region" = "\Storage\MySQL\ApiV3\Entity\Poi\Region", * "airport" = "\Storage\MySQL\ApiV3\Entity\Poi\Airport", * "station" = "\Storage\MySQL\ApiV3\Entity\Poi\Station", * "poi" = "\Storage\MySQL\ApiV3\Entity\Poi\GeneralPoi", * }) */ abstract class Poi ``` Right now this is not possible as `length=0` on a `DiscriminatorColumn` will become `length=255` and thus a diff will be reported from the schema comparison.
admin added the pull-request label 2026-01-22 16:11:15 +01:00
admin closed this issue 2026-01-22 16:11:15 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#11592