Nin*_*Nin 6 php doctrine symfony doctrine-orm symfony4
所以我有这个自定义的 Dotrine 类型
命名空间 App\Doctrine\Types;
使用 Doctrine\DBAL\Platforms\AbstractPlatform;使用 Doctrine\DBAL\Types\TextType;
class MyType extends TextType
{
private $prefix='';
public function getName()
{
return 'my_type';
}
public function setPrefix(string $prefix)
{
$this->prefix=$prefix;
}
}
Run Code Online (Sandbox Code Playgroud)
我在 config/packages/doctrine.yml 中注册:
doctrine:
dbal:
types:
my_type: App\Doctrine\Types\MyType
Run Code Online (Sandbox Code Playgroud)
然后在 Kernel boot() 中我尝试向此类型添加一些参数:
public function boot() {
parent::boot();
$myType=Type::getType('my_type');
$myType->setPrefix('abc');
}
Run Code Online (Sandbox Code Playgroud)
我第一次运行该应用程序时,效果非常好。前缀是为类型设置的,可以在整个应用程序中使用。但是,第二次我遇到异常:
请求未知的列类型“加密文本”。您使用的任何 Doctrine 类型都必须使用 \Doctrine\DBAL\Types\Type::addType() 注册。您可以使用 \Doctrine\DBAL\Types\Type::getTypesMap() 获取所有已知类型的列表。如果在数据库自省期间发生此错误,那么您可能忘记了为 Doctrine 类型注册所有数据库类型。使用 AbstractPlatform#registerDoctrineTypeMapping() 或让您的自定义类型实现 Type#getMappedDatabaseTypes()。如果类型名称为空,则可能是缓存有问题或忘记了一些映射信息。
然后我将 boot() 更改为:
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');
}
Run Code Online (Sandbox Code Playgroud)
现在异常消失了,但是前缀没有设置。我知道例外情况为我提供了有关做什么的信息,但我真的不知道从哪里开始。
有人能指出我正确的方向吗?
现在我通过从 config/packages/doctrine.yml 中删除它来修复它,因此它不再在那里注册。在内核中我现在可以加载它:
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');
}
Run Code Online (Sandbox Code Playgroud)
我仍然无法真正理解为什么这在构建缓存之前有效,但在构建缓存后却不起作用。但好吧,我现在可以继续。
如果有人有更好的答案,我很乐意接受。