I'm looking for a way to make doctrine using TIMESTAMP instead of DATETIME for MySql.
Additionaly I need to set ON UPDATE CURRENT_TIMESTAMP and CURRENT_TIMESTAMP as default values.
I would like to have the possibility to have all this code in PHP annotations to have everything in one central place.
How can I do that?
After hours of searching, I found the answer and I hope this helps anyone else looking for a more satisfactory solution than entity lifecycles and the WRONG column type (because a TIMESTAMP is a particular type with specific behaviours beyond just storing a datetime value)
All you need to do is to add
* @ORM\Column(type="datetime", nullable=false)
* @ORM\Version
to your annotation and Doctrine will both create a TIMESTAMP column with DEFAULT CURRENT_TIMESTAMP and then return the actual table value as a valid \DateTime object.
CTOP (Credits to Original Poster)
/**
* @var \DateTime
* @ORM\Column(type="datetime", columnDefinition="timestamp default current_timestamp")
*/
protected $createdAt;
/**
* @var \DateTime
* @ORM\Column(type="datetime", columnDefinition="timestamp default current_timestamp on update current_timestamp")
*/
protected $updatedAt;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With