Override DBAL Type

Viewed 531

https://www.doctrine-project.org/projects/doctrine-orm/en/2.7/cookbook/working-with-datetime.html

I tried to use this code to make an UTC DateTime type. However, the code is not being called.

What confuses me is this section:

use Doctrine\DBAL\Types\Type;
use DoctrineExtensions\DBAL\Types\UTCDateTimeType;

Type::overrideType('datetime', UTCDateTimeType::class);
Type::overrideType('datetimetz', UTCDateTimeType::class);

There is no explanation where this code should be called. It only says "before bootstrapping the ORM", which after hours of googling I still haven't figured out what that means and I'm at a dead end now.

I tried to put the code in the same file as the UTCDateTimeType class, but that didn't work.

1 Answers

With symfony, new doctrine types shall be declared in the config/doctrine.yaml file.

doctrine:
    dbal:
        types:
            datetime:   DoctrineExtensions\DBAL\Types\UTCDateTimeType
            datetimez:  DoctrineExtensions\DBAL\Types\UTCDateTimeType

Be sure that the class DoctrineExtensions\DBAL\Types\UTCDateTimeType exists.

Here is a full example to install a spatial extension and declare the geometric types.

Related