1+ <!DOCTYPE html>
2+ < html lang ="en ">
3+ < head >
4+ < title > DBAL Types</ title >
5+
6+ </ head >
7+ < body >
8+ <!-- content start -->
9+ < div class ="section " id ="dbal-types ">
10+ < h1 > DBAL Types</ h1 >
11+
12+ < p > Custom DBAL types can be registered using the < code > AsDbalType</ code > attribute. This
13+ attribute allows you to define a name for your custom type directly in the class
14+ definition.</ p >
15+
16+
17+ < p > To register a custom DBAL type, create a class that extends
18+ < code > Doctrine\DBAL\Types\Type</ code > and add the < code > #[AsDbalType]</ code > attribute to it:</ p >
19+
20+ < pre > < code class ="language-php "> namespace App\Doctrine\Type;
21+
22+ use Doctrine\Bundle\DoctrineBundle\Attribute\AsDbalType;
23+ use Doctrine\DBAL\Platforms\AbstractPlatform;
24+ use Doctrine\DBAL\Types\Type;
25+
26+ #[AsDbalType(name: 'money')]
27+ class MoneyType extends Type
28+ {
29+ public function getSQLDeclaration(array $column, AbstractPlatform $platform): string
30+ {
31+ return $platform->getDecimalTypeDeclarationSQL($column);
32+ }
33+
34+ public function convertToPHPValue($value, AbstractPlatform $platform): mixed
35+ {
36+ return $value;
37+ }
38+
39+ public function convertToDatabaseValue($value, AbstractPlatform $platform): mixed
40+ {
41+ return $value;
42+ }
43+
44+ public function getName(): string
45+ {
46+ return 'money';
47+ }
48+ }</ code > </ pre >
49+
50+
51+ < p > When using the < code > AsDbalType</ code > attribute, the type will be automatically
52+ registered with Doctrine.</ p >
53+
54+ < div class ="section " id ="manual-registration ">
55+ < h2 > Manual Registration</ h2 >
56+
57+ < p > Alternatively, you can register custom types in your configuration:</ p >
58+
59+ < div class ="configuration-block ">
60+ < div role ="tablist " aria-label ="Configuration formats " class ="configuration-tabs configuration-tabs-length-3 ">
61+ < button role ="tab " type ="button " data-language ="yaml "
62+ aria-controls ="configuration-block-tabpanel-ed4c121d12c6faa5ca20c5005d6db39d " aria-selected ="true "
63+ data-active ="true " >
64+ < span > Yaml</ span >
65+ </ button >
66+ < button role ="tab " type ="button " data-language ="xml "
67+ aria-controls ="configuration-block-tabpanel-bc5c95441c428999b3e0c4b4ffa7e792 " aria-selected ="false "
68+ tabindex ="-1 ">
69+ < span > Xml</ span >
70+ </ button >
71+ < button role ="tab " type ="button " data-language ="php "
72+ aria-controls ="configuration-block-tabpanel-4efc08d5822955550c68ac6019d85274 " aria-selected ="false "
73+ tabindex ="-1 ">
74+ < span > Php</ span >
75+ </ button >
76+ </ div >
77+
78+ < div role ="tabpanel " id ="configuration-block-tabpanel-ed4c121d12c6faa5ca20c5005d6db39d " aria-label ="Yaml " class ="configuration-codeblock " data-language ="yaml " style ="">
79+ < pre > < code class ="language-yaml "> # config/packages/doctrine.yaml
80+ doctrine:
81+ dbal:
82+ types:
83+ money: App\Doctrine\Type\MoneyType</ code > </ pre >
84+
85+ </ div >
86+ < div role ="tabpanel " id ="configuration-block-tabpanel-bc5c95441c428999b3e0c4b4ffa7e792 " aria-label ="Xml " class ="configuration-codeblock " data-language ="xml " style ="display: none ">
87+ < pre > < code class ="language-xml "> <!-- config/packages/doctrine.xml -->
88+ <container xmlns="http://symfony.com/schema/dic/services"
89+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
90+ xmlns:doctrine="http://symfony.com/schema/dic/doctrine"
91+ xsi:schemaLocation="http://symfony.com/schema/dic/services
92+ http://symfony.com/schema/dic/services/services-1.0.xsd
93+ http://symfony.com/schema/dic/doctrine
94+ http://symfony.com/schema/dic/doctrine/doctrine-1.0.xsd">
95+
96+ <doctrine:config>
97+ <doctrine:dbal>
98+ <doctrine:type name="money">App\Doctrine\Type\MoneyType</doctrine:type>
99+ </doctrine:dbal>
100+ </doctrine:config>
101+ </container></ code > </ pre >
102+
103+ </ div >
104+ < div role ="tabpanel " id ="configuration-block-tabpanel-4efc08d5822955550c68ac6019d85274 " aria-label ="Php " class ="configuration-codeblock " data-language ="php " style ="display: none ">
105+ < pre > < code class ="language-php "> // config/packages/doctrine.php
106+ use App\Doctrine\Type\MoneyType;
107+ use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
108+
109+ return static function (ContainerConfigurator $containerConfigurator): void {
110+ $containerConfigurator->extension('doctrine', [
111+ 'dbal' => [
112+ 'types' => [
113+ 'money' => MoneyType::class,
114+ ],
115+ ],
116+ ]);
117+ };</ code > </ pre >
118+
119+ </ div >
120+ </ div >
121+ </ div >
122+ </ div >
123+ <!-- content end -->
124+ </ body >
125+ </ html >
0 commit comments