Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

doctrine 2.0 : use SQL timestamp

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?

like image 898
BetaRide Avatar asked Aug 30 '12 11:08

BetaRide


2 Answers

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)

like image 72
David Soussan Avatar answered Oct 02 '22 21:10

David Soussan


/**
 * @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;
like image 21
sixty-nine Avatar answered Oct 02 '22 19:10

sixty-nine