orm
orm copied to clipboard
Carbon mapping types
Package version, Laravel version
laravel-doctrine: 1.4 laravel: 5.7
Context
I have a static and dynamic database connection. The dynamic connection is configured at runtime and the entity manager is reset after that.
Expected behaviour
An entity with a datetime
field should contain a Carbon
instance for this field because datetime
is mapped to carbondatetime
.
Actual behaviour
I have an entity handled by the static connection manager that has a datetime
field. When I inspect this entity object, this field is of type Datetime
. Only when I change it's type to carbondatetime
, the field's type is a Carbon
instance.
For the entity handled the dynamic connection manager, the same thing happens.
The mapping doesn't seem to work
Steps to reproduce the behaviour
I have installed the laravel-doctrine/extensions package and tried both registering the default service provider, and a custom service provider as mentioned here.
I have added 'carbondatetime' => \DoctrineExtensions\Types\CarbonDateTimeType::class
to doctrine.custom_types
.
For the static connection I have added 'datetime' => 'carbondatetime'
to mapping_types
. For the dynamic connection I update the mapping_types
config array right before resetting the manager because otherwise I get access denied exception (there is no valid connection config at boot).
Doctrine's documentation on custom mapping yypes, mentions that registering a doctrine type mapping is meant to be used for database schema management.
In order for the datetime
field type to be converted to carbondatetime
, you need to override it in doctrine.custom_types
.
'custom_types' => [
'date' => DoctrineExtensions\Types\CarbonDateType::class,
'datetime' => DoctrineExtensions\Types\CarbonDateTimeType::class,
'datetimetz' => DoctrineExtensions\Types\CarbonDateTimeTzType::class,
'time' => DoctrineExtensions\Types\CarbonTimeType::class,
],
As a warning, there is a bug with migrations when two types use the same Type
class. ( e.g. 'datetime'
and carbondatetime
both pointing to \DoctrineExtensions\Types\CarbonDateTimeType
). For anyone who still wants to have both carbondatetime
and datetime
use the following:
'custom_types' => [
'carbondate' => DoctrineExtensions\Types\CarbonDateType::class,
'carbondatetime' => DoctrineExtensions\Types\CarbonDateTimeType::class,
'carbondatetimetz' => DoctrineExtensions\Types\CarbonDateTimeTzType::class,
'carbontime' => DoctrineExtensions\Types\CarbonTimeType::class,
'date' => DoctrineExtensions\Types\CarbonDateType::class,
'datetime' => DoctrineExtensions\Types\CarbonDateTimeType::class,
'datetimetz' => DoctrineExtensions\Types\CarbonDateTimeTzType::class,
'time' => DoctrineExtensions\Types\CarbonTimeType::class,
],
The fix for the bug with migrations will be fixed in doctrine/dbal 2.9.3.