Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Carbon mapping types #371

Closed
kennaar opened this issue Mar 19, 2019 · 2 comments
Closed

Carbon mapping types #371

kennaar opened this issue Mar 19, 2019 · 2 comments

Comments

@kennaar
Copy link

kennaar commented Mar 19, 2019

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).

@garret-gunter
Copy link
Contributor

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,
  ],

@garret-gunter
Copy link
Contributor

The fix for the bug with migrations will be fixed in doctrine/dbal 2.9.3.

@dpslwk dpslwk closed this as completed Jan 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants