Reputation: 730
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
[Doctrine\DBAL\DBALException]
Unknown database type my_type_enum
requested, Doctrine\DBAL\Platforms\PostgreSQL92Platform
may not suppo
rt it.
Seems I need to map my custom type my_type_enum
to a string using mapping_types
, but where in Zend Expressive? It's seems my configuration is ignored
...
'doctrine' => [
'dbal' => [
'mapping_types' => [
'my_type_enum' => 'string'
]
]
]
...
Upvotes: 0
Views: 784
Reputation:
zend-expressive itself doesn't have doctrine support build in. It depends on the doctrine module and its factory that you are using. 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. 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,
], // <-----
],
];
Upvotes: 3
Reputation: 2039
First of all, you have to create your custom type that extends doctrine DBAL type:
<?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
The next step is to register the new type, let say in your doctrine bootstrap or 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;
Upvotes: 2