You are browsing a version that has not yet been released. |
Custom Mapping Types
Doctrine allows you to create new mapping types. This can come in handy when you're missing a specific mapping type or when you want to replace the existing implementation of a mapping type.
In order to create a new mapping type you need to subclass
Doctrine\DBAL\Types\Type
and implement/override the methods as
you wish. Here is an example skeleton of such a custom type class:
1 <?php
namespace My\Project\Types;
use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Platforms\AbstractPlatform;
/**
* My custom datatype.
*/
class MyType extends Type
{
const MYTYPE = 'mytype'; // modify to match your type name
public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
{
// return the SQL used to create your column type. To create a portable column type, use the $platform.
}
public function convertToPHPValue($value, AbstractPlatform $platform)
{
// This is executed when the value is read from the database. Make your conversions here, optionally using the $platform.
}
public function convertToDatabaseValue($value, AbstractPlatform $platform)
{
// This is executed when the value is written to the database. Make your conversions here, optionally using the $platform.
}
public function getName()
{
return self::MYTYPE; // modify to match your constant name
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
The following assumptions are applied to mapping types by the ORM:
|
When you have implemented the type you still need to let Doctrine
know about it. This can be achieved through the
Doctrine\DBAL\Types\Type#addType($name, $className)
method. See the following example:
To convert the underlying database type of your
new "mytype" directly into an instance of MyType
when performing
schema operations, the type has to be registered with the database
platform as well:
When registering the custom types in the configuration you specify a unique name for the mapping type and map that to the corresponding fully qualified class name. Now the new type can be used when mapping columns: