You are browsing a version that has not yet been released. |
DBAL Types
Custom DBAL types can be registered using the AsDbalType attribute. This
attribute allows you to define a name for your custom type directly in the class
definition. If the name is not provided, the class name will be used as the default.
To register a custom DBAL type, create a class that extends
Doctrine\DBAL\Types\Type and add the #[AsDbalType] attribute to it:
namespace App\Doctrine\Type;use Doctrine\Bundle\DoctrineBundle\Attribute\AsDbalType;use Doctrine\DBAL\Platforms\AbstractPlatform;use Doctrine\DBAL\Types\Type;#[AsDbalType(name: 'money')]class MoneyType extends Type{ public function getSQLDeclaration(array $column, AbstractPlatform $platform): string { return $platform->getDecimalTypeDeclarationSQL($column); } public function convertToPHPValue(mixed $value, AbstractPlatform $platform): mixed { return $value; } public function convertToDatabaseValue(mixed $value, AbstractPlatform $platform): mixed { return $value; }}
When using the AsDbalType attribute, the type will be automatically
registered with Doctrine.
Manual Registration
Alternatively, you can register custom types in your configuration:
# config/packages/doctrine.yamldoctrine: dbal: types: money: App\Doctrine\Type\MoneyType
