Skip to content
Advertisement

Doctrine – Add default time stamp to entity like NOW()

Following the Doctrine guidelines I understand how to set a default value for an Entity, but what if I wanted a date/time stamp?

My problem is my database has a default of NOW() on a field but when I use Doctrine to insert a record the values are null or blank but the rest of the insert happened.

Also since Doctrine says to declare the default as a const, this also creates a problem.

Suggestions?

Advertisement

Answer

Ok I found the solution:

The prePersist option is what I’m doing.

Make sure you define in the annotations

JavaScript

and here is the function example they offer

JavaScript

And if you’re using ORM like I am

JavaScript

and here is the function example they offer

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement