Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override Doctrine2 types in Symfony3

I would like to use Carbon objects in my Symfony 3.2 application instead of the SPL \DateTime object. I found a set of DoctrineExtension classes in here.

Edited my config.yml file:

doctrine:
    dbal:
        ...
        types:
            carbondatetime: DoctrineExtensions\Types\CarbonDateTimeType
            carbondate: DoctrineExtensions\Types\CarbonDateType
            carbontime: DoctrineExtensions\Types\CarbonTimeType
        mapping_types:
            datetime: carbondatetime
            date: carbondate
            enum: string
            time: carbontime

I successfully check if the types are loaded with:

Doctrine\DBAL\Types\Type::getTypesMap()

And the mapping as well is working properly (returns carbondatetime):

$this->getDoctrine()->getManager()
     ->getConnection()->getDatabasePlatform()
     ->getDoctrineTypeMapping('datetime');

I execute a query over a Doctrine repository and still get DateTime objects. It is working in 2 cases:

  • Changing the Entity to @ORM\Column(type="carbondatetime")
  • Or executing the following code \Doctrine\DBAL\Types\Type::overrideType('datetime', 'DoctrineExtensions\Types\CarbonDateTimeType');

Is there a Best-Practice to override the Doctrine DBAL types? Preferably in the YAML configuration.

Thanks

like image 993
binzram Avatar asked Dec 18 '22 08:12

binzram


1 Answers

Woah.... Like always, as soon you ask the question you find the solution:

doctrine:
    dbal:
        ...
        types:
            datetime: DoctrineExtensions\Types\CarbonDateTimeType
            date: DoctrineExtensions\Types\CarbonDateType
            time: DoctrineExtensions\Types\CarbonTimeType
        mapping_types:
            enum: string
like image 114
binzram Avatar answered Jan 02 '23 02:01

binzram