How to get Doctrine handling ENUMs correctly?

2019-05-11 21:54发布

问题:

In an application I have a case of the Class Table Inheritance. The discriminator column is an ENUM:

/**
 * Foo
 *
 * @ORM\Table(name="foos", ...)
 * @ORM\Entity
 * @ORM\InheritanceType("JOINED")
 * @ORM\DiscriminatorColumn(name="`type`", type="string", columnDefinition="ENUM('bar', 'buz')")
 * @ORM\DiscriminatorMap({
 *     "bar" = "Bar",
 *     "buz" = "Buz"
 * })
 */
abstract class Foo
{
    ...
}

Doctrine works as expected (to begin with). The doctrine:migrations:diff command creates a migration for the tables and relationships and also defines the discriminator column correctly, as an ENUM.

Then I execute the migrations (doctrine:migrations:migrate). The schema looks well. But:

When I execute the diff command again (and expect no new migrations), I get a new migration generated:

final class Version20180619205625 extends AbstractMigration
{
    public function up(Schema $schema) : void
    {
        $this->addSql('ALTER TABLE foos CHANGE type `type` ENUM(\'bar\', \'buz\')');
    }

    public function down(Schema $schema) : void
    {
        $this->addSql('ALTER TABLE tasks CHANGE `type` type VARCHAR(255) DEFAULT NULL COLLATE utf8mb4_unicode_ci');
    }
}

Alright, I execute it. And try the diff command again. And get the same migration generated again... So, Doctrine seems to "think", the column is still VARCHAR.

I showed the issue here on example of an inheritance discriminator. But actually it doesn't matter, if the column is a discriminator or not -- this behavior is the same for every ENUM column.

How to solve this issue? Is there a way make Doctrine handle ENUM columns correctly?