Skip to content
Newer
Older
100644 104 lines (76 sloc) 3.15 KB
546c817 @beberlei Move Custom Mapping Types into cookbook, restructure the chapter more.
beberlei authored
1 Custom Mapping Types
2 ====================
3
4 Doctrine allows you to create new mapping types. This can come in
5 handy when you're missing a specific mapping type or when you want
6 to replace the existing implementation of a mapping type.
7
8 In order to create a new mapping type you need to subclass
9 ``Doctrine\DBAL\Types\Type`` and implement/override the methods as
10 you wish. Here is an example skeleton of such a custom type class:
11
12 .. code-block:: php
13
14 <?php
15 namespace My\Project\Types;
904bcfa @stefanotorresi add docs notice about custom object types as IDs
stefanotorresi authored
16
546c817 @beberlei Move Custom Mapping Types into cookbook, restructure the chapter more.
beberlei authored
17 use Doctrine\DBAL\Types\Type;
18 use Doctrine\DBAL\Platforms\AbstractPlatform;
904bcfa @stefanotorresi add docs notice about custom object types as IDs
stefanotorresi authored
19
546c817 @beberlei Move Custom Mapping Types into cookbook, restructure the chapter more.
beberlei authored
20 /**
21 * My custom datatype.
22 */
23 class MyType extends Type
24 {
25 const MYTYPE = 'mytype'; // modify to match your type name
904bcfa @stefanotorresi add docs notice about custom object types as IDs
stefanotorresi authored
26
228dc89 @Wilt getSqlDeclaration -> getSQLDeclaration
Wilt authored
27 public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
546c817 @beberlei Move Custom Mapping Types into cookbook, restructure the chapter more.
beberlei authored
28 {
29 // return the SQL used to create your column type. To create a portable column type, use the $platform.
30 }
904bcfa @stefanotorresi add docs notice about custom object types as IDs
stefanotorresi authored
31
546c817 @beberlei Move Custom Mapping Types into cookbook, restructure the chapter more.
beberlei authored
32 public function convertToPHPValue($value, AbstractPlatform $platform)
33 {
34 // This is executed when the value is read from the database. Make your conversions here, optionally using the $platform.
35 }
904bcfa @stefanotorresi add docs notice about custom object types as IDs
stefanotorresi authored
36
546c817 @beberlei Move Custom Mapping Types into cookbook, restructure the chapter more.
beberlei authored
37 public function convertToDatabaseValue($value, AbstractPlatform $platform)
38 {
39 // This is executed when the value is written to the database. Make your conversions here, optionally using the $platform.
40 }
904bcfa @stefanotorresi add docs notice about custom object types as IDs
stefanotorresi authored
41
546c817 @beberlei Move Custom Mapping Types into cookbook, restructure the chapter more.
beberlei authored
42 public function getName()
43 {
44 return self::MYTYPE; // modify to match your constant name
45 }
46 }
47
904bcfa @stefanotorresi add docs notice about custom object types as IDs
stefanotorresi authored
48 .. note::
49
50 The following assumptions are applied to mapping types by the ORM:
546c817 @beberlei Move Custom Mapping Types into cookbook, restructure the chapter more.
beberlei authored
51
904bcfa @stefanotorresi add docs notice about custom object types as IDs
stefanotorresi authored
52 - If the value of the field is *NULL* the method
53 ``convertToDatabaseValue()`` is not called.
54 - The ``UnitOfWork`` never passes values to the database convert
55 method that did not change in the request.
56 - The ``UnitOfWork`` internally assumes that entity identifiers are
57 castable to string. Hence, when using custom types that map to PHP
58 objects as IDs, such objects must implement the ``__toString()`` magic
59 method.
546c817 @beberlei Move Custom Mapping Types into cookbook, restructure the chapter more.
beberlei authored
60
61 When you have implemented the type you still need to let Doctrine
62 know about it. This can be achieved through the
63 ``Doctrine\DBAL\Types\Type#addType($name, $className)``
64 method. See the following example:
65
66 .. code-block:: php
67
68 <?php
69 // in bootstrapping code
70
71 // ...
72
73 use Doctrine\DBAL\Types\Type;
74
75 // ...
76
77 // Register my type
78 Type::addType('mytype', 'My\Project\Types\MyType');
79
80 To convert the underlying database type of your
81 new "mytype" directly into an instance of ``MyType`` when performing
82 schema operations, the type has to be registered with the database
83 platform as well:
84
85 .. code-block:: php
86
87 <?php
88 $conn = $em->getConnection();
89 $conn->getDatabasePlatform()->registerDoctrineTypeMapping('db_mytype', 'mytype');
90
91 When registering the custom types in the configuration you specify a unique
92 name for the mapping type and map that to the corresponding fully qualified
93 class name. Now the new type can be used when mapping columns:
94
95 .. code-block:: php
96
97 <?php
98 class MyPersistentClass
99 {
100 /** @Column(type="mytype") */
101 private $field;
102 }
103
Something went wrong with that request. Please try again.