在删除列时使用doctrine迁移

时间:2016-12-12 14:09:06

标签: doctrine-orm zend-framework2

我们在enumFooType添加了名为\Doctrine\DBal\Types\Type::addType()的枚举的列类型。

运行vendor/bin/doctrine-module migrations:diff以生成将删除该列的迁移时,会引发错误:

[Doctrine\DBAL\DBALException]
  Unknown column type "enumFooType" requested. Any Doctrine type that you use has to be registered with \Doctrine\DBAL\Types\Type::addType(). 
  You can get a list of all the known types with \Doctrine\DBAL\Types\Type::getTypesMap(). 
  If this error occurs during database introspection then you might have forgot to register all database types for a Doctrine Type. 
  Use AbstractPlatform#registerDoctrineTypeMapping() or have your custom types implement Type#getMappedDatabaseTypes(). 
  If the type name is empty you might have a problem with the cache or forgot some mapping information.

我猜错了,因为数据库的foo_type标有(DC2Type:enumFooType)

处理这些类型的删除的正确方法是什么?我的第一个想法是使用vendor/bin/doctrine-module migrations:generate生成空白迁移并手动编写查询,但我想要一种更自动化的方式,如果可能的话,不要手动编写任何内容。

1 个答案:

答案 0 :(得分:0)

<强> TL; DR:

在运行doctrine命令之前,DBAL类型enumFooType的类定义应该存在(现在我已经写了这行,感觉很明显,比如&#34; duh!&#34;)。

答案很长:

经过几次回滚以及试验和错误后,我为这种操作设计了以下程序:

  1. 从实体类中删除enumFooType的属性。
  2. 创建迁移(到目前为止,EnumFooType文件仍然存在)。
  3. 删除包含此dbal类型定义的EnumFooType类。
  4. 必须在此顺序中完成的原因是因为如果先删除类型,则不会加载doctrine因为此文件丢失,导致发布的异常原来的问题。

    此外,在您创建了迁移后,然后删除了该类型;如果您需要回滚该更改,则必须:

    1. 恢复到上一次提交,以便EnumFooType存在,并且在实体类中定义类型enumFooType的属性。
    2. 运行迁移命令以回滚。