laravel-doctrine / extensions

Extensions integration for Doctrine2 and Laravel
http://laraveldoctrine.org/
MIT License
48 stars 24 forks source link

Timestampable Extension + Carbon troubles #62

Open jrean opened 1 year ago

jrean commented 1 year ago

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.