doctrine 2.0 : use SQL timestamp

/**
 * @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;

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)


As suggested by Daniel Criconet,

@ORM\Column(type="datetime", columnDefinition="TIMESTAMP DEFAULT CURRENT_TIMESTAMP")

will make that particular column in the corresponding MySQL table become TIMESTAMP instead of DATETIME.

For YAML version, see the original answer.


There is no such thing like using TIMESTAMP in mysql with Doctrine. However, I fixed it myself but have to test it:

  • Create a file: Doctrine\DBAL\Types\TimestampType.php
  • Copy the code from TimeType.php into TimestampType.php
  • Rename 'Time' to 'Timestamp' in the code
  • Create a new constant, and add timestamp to the typesmap in: Doctrine\DBAL\Types\Type.php
  • Create function called getTimestampTypeDeclarationSQL in Doctrine\DBAL\Platforms\AbstractPlatform.php
  • And in that function, return TIMESTAMP

I've used yaml to create my Entities and Proxies, so use in yaml:

type: timestamp

I'm going to test this 'fix'/'workaround' now. I'll let you know.