在Symfony4中注册自定义主义类型 [英] Register custom Doctrine type in Symfony4

查看:71
本文介绍了在Symfony4中注册自定义主义类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有这个自定义的教义类型

So I have this custom Doctrine type

命名空间App\Doctrine\Types;

namespace App\Doctrine\Types;

使用原则\DBAL\平台\抽象平台;
使用Doctrine\DBAL\Types\TextType;

use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Types\TextType;

class MyType extends TextType
{
   private $prefix='';

   public function getName()
   {
      return 'my_type';
   }
   public function setPrefix(string $prefix)
   {
      $this->prefix=$prefix;
   }
}

我在config / packages / doctrine中注册。 yml:

I registerd in in the config/packages/doctrine.yml:

doctrine:
    dbal:
        types:
            my_type: App\Doctrine\Types\MyType

然后在内核boot()中,我试图向其中添加一些参数这种类型:

Then in Kernel boot() I'm trying to add some parameters to this type:

public function boot() {
   parent::boot();

   $myType=Type::getType('my_type');
   $myType->setPrefix('abc');
}

这在我第一次运行该应用程序时非常有效。为类型设置前缀,并且可以在整个应用程序中使用。但是,第二次出现异常:

This works perfectly the first time I run the app. The prefix is set for the type and can be used through the whole app. However, the second time I get an Exception:


未知列类型 encrypted_text要求。您使用的任何教义
类型都必须向
\Doctrine\DBAL\Types\Type :: addType()注册。您可以使用\Doctrine\DBAL\Types\Type :: getTypesMap()获得所有
已知类型的列表。如果在数据库自省期间发生此
错误,则可能会忘记
来为教义类型注册所有数据库类型。使用
AbstractPlatform#registerDoctrineTypeMapping()或让您的自定义
类型实现Type#getMappedDatabaseTypes()。如果类型名称为
为空,则可能是缓存出现问题或忘记了某些映射
信息。

Unknown column type "encrypted_text" 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 forgotten 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.

然后我将boot()更改为:

I then changed boot() to :

    public function boot() {
       parent::boot();
       if (!Type::hasType('my_type')) {
           Type::addType('my_type', 'App\Doctrine\Types\MyType');
       }
       $myType=Type::getType('my_type');
       $myType->setPrefix('abc');
   }

现在,异常消失了,但是没有设置前缀。我知道例外情况为我提供了有关操作的信息,但我真的不知道从哪里开始。

Now the exception is gone, but the prefix is not set. I know the exceptions gives me information about what to do but I really don't know where to start.

有人能指出我正确的方向吗?

Can anyone point me in the right direction?

推荐答案

现在,我通过从config / packages / doctrine.yml中删除它来修复它,因此不再在其中注册。在内核中,我现在可以加载它:

For now I fixed it by removing it from config/packages/doctrine.yml so it's not registered there anymore. In Kernel I can now load it:

    public function boot() {
       parent::boot();
       if (!Type::hasType('my_type')) {
           Type::addType('my_type', 'App\Doctrine\Types\MyType');
       }
       $myType=Type::getType('my_type');
       $myType->setPrefix('abc');
   }

在构建缓存之前,我仍然无法真正理解为什么这样做,但是一旦建立缓存。但是,我现在可以继续。

I still can't really understand why this works before building the cache but not once the cache is build. But well, I can continue now.

如果有人有更好的答案,我会很乐意接受。

If someone has a better answer, I'd be more than happy to accept it.

这篇关于在Symfony4中注册自定义主义类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆