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

Timestampable Extension + Carbon troubles #62

Open
jrean opened this issue Jun 18, 2023 · 0 comments
Open

Timestampable Extension + Carbon troubles #62

jrean opened this issue Jun 18, 2023 · 0 comments

Comments

@jrean
Copy link

jrean commented Jun 18, 2023

Hi,

laravel-doctrine/extensions 1.5.0
laravel-doctrine/orm 1.7.13
beberlei/doctrineextensions 1.3.0
gedmo/doctrine-extensions 3.2.0

I use the TimestampableExtension and I use in my Entity the trait LaravelDoctrine\Extensions\Timestamps\Timestamps.
I want to use Carbon ; following the documentation, I added a custom_type in the the custom_types key of the doctrine.php config file:

'datetime' => Carbon\Doctrine\DateTimeType::class,

// Also works..
// 'datetime' => DoctrineExtensions\Types\CarbonDateTimeType::class,

As expected, when I query the database using the entity manager, my createdAt / updatedAt property (or any other property of type DateTime) is of type Carbon. Great!

BUT if I instantiate a new Entity and persist it as follow:

$foo = new Foo();
$entityManager->persist($foo);

$foo->getCreatedAt() // DateTime

Both createdAt and updatedAt are of type DateTime. So it was set using DateTime and not Carbon..

How can I have it to be of type Carbon instead of DateTime? The reason I'm asking is because while writing unit tests, I want to be able to travel back and forth in time using Laravel travel() sugar or just by doing Carbon::setTestNow(now()->subDays(2)); so when I create/persist my entity the created_at value will be automatically of what was set as "now" in the test since it uses Carbon end-to-end.

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

1 participant