简体   繁体   English

zend表现力+原则自定义类型

[英]zend expressive + doctrine custom types

I'm trying to map a custom type to a string. 我正在尝试将自定义类型映射到字符串。 Here's my entity definition: 这是我的实体定义:

/**
 * @var string
 * 
 * @ORM\Column(name="type", type="string", columnDefinition="my_type_enum", nullable=false)
 */

But when I try to create migration (migration:diff) this is the output 但是,当我尝试创建迁移(migration:diff)时,这是输出

[Doctrine\\DBAL\\DBALException] Unknown database type my_type_enum requested, Doctrine\\DBAL\\Platforms\\PostgreSQL92Platform may not suppo rt it. [Doctrine \\ DBAL \\ DBALException]请求了未知的数据库类型my_type_enumDoctrine\\DBAL\\Platforms\\PostgreSQL92Platform可能不支持它。

Seems I need to map my custom type my_type_enum to a string using mapping_types , but where in Zend Expressive? 似乎我需要使用mapping_types将自定义类型my_type_enum到字符串,但是Zend Expressive中的何处? It's seems my configuration is ignored 看来我的配置被忽略了

...
     'doctrine' => [
       'dbal' => [
         'mapping_types' => [
           'my_type_enum' => 'string'
         ]
       ]
     ]
...

zend-expressive itself doesn't have doctrine support build in. It depends on the doctrine module and its factory that you are using. zend-expressive本身没有内置的学说支持。它取决于您所使用的学说模块及其工厂。 The factory starts the doctrine service with the config. 工厂使用配置启动理论服务。 So I would look inside the doctrine factory to figure out how and if it supports custom mapping types. 因此,我将深入研究原则工厂,以了解其如何以及是否支持自定义映射类型。

In case yours doesn't support it, you can use container-interop-doctrine . 如果您的服务器不支持它,则可以使用container-interop-doctrine It has support for this built in it seems (haven't tried it myself): 它似乎对此内置了支持(我自己还没有尝试过):

<?php
return [
    'doctrine' => [
        // ...
        'connection' => [
            'orm_default' => [
                'driver_class' => \Doctrine\DBAL\Driver\PDOMySql\Driver::class,
                'wrapper_class' => null,
                'pdo' => null,
                'configuration' => 'orm_default', 
                'event_manager' => 'orm_default', 
                'params' => [],
                'doctrine_mapping_types' => [], // <-----
                'doctrine_commented_types' => [],
            ],
        ],
        'types' = [
            'typename' => Type::class,
        ], // <-----
    ],
];

First of all, you have to create your custom type that extends doctrine DBAL type: 首先,您必须创建扩展了理论DBAL类型的自定义类型:

<?php
use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\ConversionException;

class MyType extends Type
{
    const MYTYPE = 'mytype';

    public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform) {
        return 'mytype';
    }

    public function convertToPHPValue($value, AbstractPlatform $platform) {
        // convert your type to php value
    }

    public function convertToDatabaseValue($value, AbstractPlatform $platform) {
        // convert your type to database value
    }
}

Recently I integrated a value object as a doctrine type, so you can take a look how your new type should look like: PostcodeType 最近,我将值对象集成为一种学说类型,因此您可以看一下新类型的外观: PostcodeType

The next step is to register the new type, let say in your doctrine bootstrap or EntityManagerFactory: 下一步是注册新类型,在您的学说引导程序或EntityManagerFactory中说:

<?php // ./src/Container/EntityManagerFactory.php

if (!\Doctrine\DBAL\Types\Type::hasType("mytype")) {
    \Doctrine\DBAL\Types\Type::addType('mytype', 'Your\Namespace\MyType');
    $em->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('mytype', 'mytype');
}

return $em;

Finally you have registered your new type and you can use it: 最后,您已经注册了新类型,可以使用它:

/**
 * @var \Your\Namespace\MyType
 * @Column(type="mytype")
 */
protected $param;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM