Doctrine2:类型x已经存在

Bap*_*aux 3 api types doctrine

我有Doctrine API的问题.

我想添加一个新的Doctrine Type.我按照此文档创建了类,并在自定义驱动程序中添加了类型.

Type::addType("custom", "Namespace\NameBundle\Types\CustomType");
$this->registerDoctrineTypeMapping("CustomType", "custom");
Run Code Online (Sandbox Code Playgroud)

当我执行php app/console cache时,我的问题会附加:clear.

[Doctrine\DBAL\DBALException]
Type custom already exists.
Run Code Online (Sandbox Code Playgroud)

经过几次搜索,我发现在Doctrine\DBAL\Types\Type :: addType(...)中如果知道类型就抛出异常...我不明白为什么会抛出这个错误.

Bap*_*aux 18

我发现了我的问题!

我不知道为什么,但我的自定义类型一次又一次加载.

要解决此问题,请添加此代码,例如检查.

if (!Type::hasType("custom")) {
    Type::addType("custom", "Namespace\NameBundle\Types\CustomType");
    $this->registerDoctrineTypeMapping("CustomType", "custom");
}
Run Code Online (Sandbox Code Playgroud)

有用 !