Could someone tell me how to add default value on datetime column? I can't do this like this:
protected $registration_date = date("Y-m-d H:i:s", time());
So how?
Could someone tell me how to add default value on datetime column? I can't do this like this:
protected $registration_date = date("Y-m-d H:i:s", time());
So how?
Work for me with MySql and Symfony 3.4.
I think, the best way to accomplish autofill for
datetime
is to make like that:Putting logic to constructor isn't right solution, because setting default values are SQL client responsibility. If you decide no longer use ORM - you will lost business logic. Plus, if using constructor you won't be able to add default timestamps to
datetime
attributes for existing rows.This will work. Just posting for future ref.
There is an extension for this automating this...
https://github.com/l3pp4rd/DoctrineExtensions/blob/master/doc/timestampable.md
You can also use lifecycle callbacks if you want to be very precise:
You map your property as DateTime type then set the value in the constructor using a new DateTime instance:
This works as the constructor of a persisted class is not called upon hydration.