【问题标题】:The Doctrine make a not correct diff for an entity with ENUM typeDoctrine 对 ENUM 类型的实体做出了不正确的差异
【发布时间】:2020-07-27 21:04:49
【问题描述】:

我为 cookbook 的实体 Command 的属性 type 做了一个特殊的 ENUM 类型。


该属性如下所示:

    /**
     * @var CommandType
     *
     * @ORM\Column(type="command_type")
     */
    protected $type;


这个 sn-p 描述了新的学说类型:

final class CommandTypeType extends EnumerableType
{
    public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
    {
        return 'VARCHAR(30)';
    }

    public function getName(): string
    {
        return 'command_type';
    }

    protected function getClassName(): string
    {
        return CommandType::class;
    }
}


在第一次为实体运行命令 /bin/console doctrine:migrations:diff 后,我得到了迁移,它看起来是正确的

final class Version2020 072720500 extends AbstractMigration
{
    public function up(Schema $schema) : void
    {
        $this->addSql("CREATE TABLE commands (
                id INT AUTO_INCREMENT NOT NULL,
                name VARCHAR(30) DEFAULT NULL,
                command VARCHAR(100) DEFAULT NULL,
                type varchar(30) NOT NULL,
                PRIMARY KEY(id)
        ) DEFAULT CHARACTER SET UTF8 COLLATE `UTF8_unicode_ci` ENGINE = InnoDB");
    }

    public function down(Schema $schema) : void
    {
        $this->addSql('DROP TABLE commands');
    }
}


下一步是运行命令/bin/console doctrine:migrations:migrate,它工作正常,表已创建。

然后我再次运行命令/bin/console doctrine:migrations:diff 并获得新的迁移

final class Version20200727205035 extends AbstractMigration
{
    public function up(Schema $schema) : void
    {
        $this->addSql('ALTER TABLE commands CHANGE type type VARCHAR(30) NOT NULL');
    }

    public function down(Schema $schema) : void
    {
        $this->addSql('ALTER TABLE commands CHANGE type type VARCHAR(30) CHARACTER SET utf8 NOT NULL COLLATE `utf8_unicode_ci`');
    }
}


我不知道为什么第二个差异会产生这种奇怪的迁移。 我做错了什么?

  • Symfony 4.4
  • doctrine/doctrine-migrations-bundle 3.0.1
  • 学说/迁移 3.0.1

【问题讨论】:

    标签: symfony doctrine-orm doctrine


    【解决方案1】:

    Doctrine 需要在列中添加注释以检测自定义类型已应用于列。

    检查 requiresSQLCommentHint 方法是否已实现并返回 true 或将其添加到您的自定义类型中。

    final class CommandTypeType extends EnumerableType
    {
        ...
    
        public function requiresSQLCommentHint(AbstractPlatform $platform): bool
        {
            return true;
        }
    }
    

    您应该会看到将在下次迁移时将评论添加到您的专栏中,仅此而已。

    【讨论】:

    • 我在 ENUM 中添加了一个新属性并运行了doctrine:migrations:diff,但是没有出现新的更改,请问您知道如何修复它吗?
    • 从 ORM 的角度来看没有任何改变,您必须手动添加它来生成迁移。您总是可以尝试更改列属性(例如长度)以使教义认为某些内容已更改,但我不确定它是否会起作用。
    猜你喜欢
    • 1970-01-01
    • 2018-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-27
    • 1970-01-01
    • 2014-08-25
    相关资源
    最近更新 更多