Could someone tell me how to add the default value on a DateTime column? I can't do it like this:
protected $registration_date = date("Y-m-d H:i:s", time()); So how can I handle it?
For default value CURRENT_TIMESTAMP:
@ORM\Column(name="created_at", type="datetime", options={"default": "CURRENT_TIMESTAMP"}) Or for older Symfony versions:
@ORM\Column(name="created_at", type="datetime", options={"default": 0}) Worked for me... However this works only with MySQL.
options={"default": "CURRENT_TIMESTAMP"}) before which generally worked aswell. But with my postgres db behind it it got transformed to DEFAULT NOW() which is the postgres way. But then a doctrine:migrations:diff would misinterpret the now() as a deviation from CURRENT_TIMESTAMP and create a diff file each time I run it. But with this solution doctrine gets that everything is in sync.ADD date_added TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT CURRENT_TIMESTAMP NOT NULL for me.SQLSTATE[42000]: Syntax error or access violation: 1067 Invalid default value for 'date_create'You map your property as DateTime type then set the value in the constructor using a new DateTime instance:
/** * @Entity * @Table(name="...") */ class MyEntity { /** @Column(type="datetime") */ protected $registration_date; public function __construct() { $this->registration_date = new DateTime(); } } This works as the constructor of a persisted class is not called upon hydration.
onPrePersistSetRegistrationDate which add HasLifecycleCallbacks, PrePersist annotations 2) I want to set a default value needs me to add 3rd library @Gedmo\Timestampable?!! 3) CURRENT_TIMESTAMP is sometimes not good. Ex.: when your script server in China but database server in America. ( of cause, it is not very common, but there are such cases )You can also use lifecycle callbacks if you want to be very precise:
use Doctrine\ORM\Mapping as ORM; /** * @ORM\HasLifecycleCallbacks * ... */ class MyEntity { /** * @ORM\PrePersist */ public function onPrePersistSetRegistrationDate() { $this->registration_date = new \DateTime(); } } I think, the best way to accomplish autofill for datetime is to make like that:
* @ORM\Column(type="datetime", options={"default"="CURRENT_TIMESTAMP"}) 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.
"default": 0 works better with doctrine migrations.There is an extension for this automating this...
https://github.com/l3pp4rd/DoctrineExtensions/blob/master/doc/timestampable.md
/** * @var \DateTime * * @ORM\Column(name="date_added", type="datetime") * @Gedmo\Timestampable(on="create") */ private $date_added; /** * @var \DateTime * * @ORM\Column(name="date_modified", type="datetime") * @Gedmo\Timestampable(on="update") */ private $date_modified; @var string @ORM\Column(name="login_at", type="datetime", options={"default" = "CURRENT_TIMESTAMP"}) This will work. Just posting for future ref.