8000 can not set default value to enum case · Issue #9375 · doctrine/orm · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
can not set default value to enum case #9375
Closed
@jgroc-de

Description

@jgroc-de

Bug Report

Q A
BC Break no
Version 2.11.0

Summary

We dumbly tried to put a default value to a property set to a enum's case and
it does not work when generating the migration
which means we can not set default value based on enum case
(we can hardcode it but we are not happy with this solution)

Current behavior

given an enum:

enum Suit: string {
    case Hearts = 'hearts';
    case Diamonds = 'diamonds'; 
}

trying

    /**
     * @ORM\Column(
     *      type="string",
     *      length=255,
     *      enumType="App\Enum\Suit",
     *      options={"default": App\Enum\Suit::Hearts}
     * )
     */
    private Suit $suit = Suit::Hearts;

give us after a bin/console make:migration

In AbstractPlatform.php line 3913:
                                                                                                                      
  str_replace(): Argument #3 ($subject) must be of type array|string, App\Enum\Suit given 

trying

    /**
     * @ORM\Column(
     *      type="string",
     *      length=255,
     *      enumType="App\Enum\Suit",
     *      options={"default": "App\Enum\Suit::Hearts"}
     * )
     */
    private Suit $suit = Suit::Hearts;

give us a migration like

    public function up(Schema $schema): void
    {
        // this up() migration is auto-generated, please modify it to your needs
        $this->addSql('ALTER TABLE entity ALTER suit SET DEFAULT \'App\\Enum\\Suit::Hearts\'');
    }

which is the string "App\Enum\Suit::Hearts" and not the value of the enum case

How to reproduce

create an enum:

enum Suit: string {
    case Hearts = 'hearts';
    case Diamonds = 'diamonds'; 
}

set a property in an entity linking to this enum
with a default value set to one of the enum case

    /**
     * @ORM\Column(
     *      type="string",
     *      length=255,
     *      enumType="App\Enum\Suit",
     *      options={"default": App\Enum\Suit::Hearts}
     * )
     */
    private Suit $suit = Suit::Hearts;

make a migration
(in sf5): bin/console make:migration

Expected behavior

trying

    /**
     * @ORM\Column(
     *      type="string",
     *      length=255,
     *      enumType="App\Enum\Suit",
     *      options={"default": App\Enum\Suit::Hearts}
     * )
     */
    private Suit $suit = Suit::Hearts;

give us a migration like

    public function up(Schema $schema): void
    {
        // this up() migration is auto-generated, please modify it to your needs
        $this->addSql('ALTER TABLE entity ALTER suit SET DEFAULT \'hearts\''');
    }

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0