How to get Doctrine handling ENUMs correctly?

折月煮酒 提交于 2019-12-07 14:18:13

问题


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?

来源:https://stackoverflow.com/questions/50937197/how-to-get-doctrine-handling-enums-correctly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!