|
| 1 | +DBAL Types |
| 2 | +========== |
| 3 | + |
| 4 | +Custom DBAL types can be registered using the ``AsDbalType`` attribute. This |
| 5 | +attribute allows you to define a name for your custom type directly in the class |
| 6 | +definition. |
| 7 | + |
| 8 | +To register a custom DBAL type, create a class that extends |
| 9 | +``Doctrine\DBAL\Types\Type`` and add the ``#[AsDbalType]`` attribute to it: |
| 10 | + |
| 11 | +.. code-block:: php |
| 12 | +
|
| 13 | + namespace App\Doctrine\Type; |
| 14 | +
|
| 15 | + use Doctrine\Bundle\DoctrineBundle\Attribute\AsDbalType; |
| 16 | + use Doctrine\DBAL\Platforms\AbstractPlatform; |
| 17 | + use Doctrine\DBAL\Types\Type; |
| 18 | +
|
| 19 | + #[AsDbalType(name: 'money')] |
| 20 | + class MoneyType extends Type |
| 21 | + { |
| 22 | + public function getSQLDeclaration(array $column, AbstractPlatform $platform): string |
| 23 | + { |
| 24 | + return $platform->getDecimalTypeDeclarationSQL($column); |
| 25 | + } |
| 26 | +
|
| 27 | + public function convertToPHPValue($value, AbstractPlatform $platform): mixed |
| 28 | + { |
| 29 | + return $value; |
| 30 | + } |
| 31 | +
|
| 32 | + public function convertToDatabaseValue($value, AbstractPlatform $platform): mixed |
| 33 | + { |
| 34 | + return $value; |
| 35 | + } |
| 36 | +
|
| 37 | + public function getName(): string |
| 38 | + { |
| 39 | + return 'money'; |
| 40 | + } |
| 41 | + } |
| 42 | +
|
| 43 | +When using the ``AsDbalType`` attribute, the type will be automatically |
| 44 | +registered with Doctrine. |
| 45 | + |
| 46 | +Manual Registration |
| 47 | +------------------- |
| 48 | + |
| 49 | +Alternatively, you can register custom types in your configuration: |
| 50 | + |
| 51 | +.. configuration-block:: |
| 52 | + |
| 53 | + .. code-block:: yaml |
| 54 | +
|
| 55 | + # config/packages/doctrine.yaml |
| 56 | + doctrine: |
| 57 | + dbal: |
| 58 | + types: |
| 59 | + money: App\Doctrine\Type\MoneyType |
| 60 | +
|
| 61 | + .. code-block:: xml |
| 62 | +
|
| 63 | + <!-- config/packages/doctrine.xml --> |
| 64 | + <container xmlns="http://symfony.com/schema/dic/services" |
| 65 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 66 | + xmlns:doctrine="http://symfony.com/schema/dic/doctrine" |
| 67 | + xsi:schemaLocation="http://symfony.com/schema/dic/services |
| 68 | + http://symfony.com/schema/dic/services/services-1.0.xsd |
| 69 | + http://symfony.com/schema/dic/doctrine |
| 70 | + http://symfony.com/schema/dic/doctrine/doctrine-1.0.xsd"> |
| 71 | +
|
| 72 | + <doctrine:config> |
| 73 | + <doctrine:dbal> |
| 74 | + <doctrine:type name="money">App\Doctrine\Type\MoneyType</doctrine:type> |
| 75 | + </doctrine:dbal> |
| 76 | + </doctrine:config> |
| 77 | + </container> |
| 78 | +
|
| 79 | + .. code-block:: php |
| 80 | +
|
| 81 | + // config/packages/doctrine.php |
| 82 | + use App\Doctrine\Type\MoneyType; |
| 83 | + use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; |
| 84 | +
|
| 85 | + return static function (ContainerConfigurator $containerConfigurator): void { |
| 86 | + $containerConfigurator->extension('doctrine', [ |
| 87 | + 'dbal' => [ |
| 88 | + 'types' => [ |
| 89 | + 'money' => MoneyType::class, |
| 90 | + ], |
| 91 | + ], |
| 92 | + ]); |
| 93 | + }; |
0 commit comments