持久化symfony2实体datetime字段:类DateTime的对象无法转换为int

I have an entity with this field:

  /**
   * @var \DateTime
   *
   * @Groups({"list", "details"})
   * @ORM\Column(name="timestamp", type="datetime")
   */
  private $timestamp;

And I have the following code:

$date = new \DateTime();
$myentity->setTimestamp($date); //Fails
$myentity->setTimestamp($date->getTimestamp()); //Also fails

I get the following error:

[Symfony\Component\Debug\Exception\ContextErrorException]       
  Notice: Object of class DateTime could not be converted to int 

What is the proper way to store a datetime field with symfony and doctrine?

There is possible than the oriented object method : $date = new \DateTime(); $myentity->setTimestamp($date->getTimestamp()); will be override by ORM.

See DateTime::getTimestamp

  1. If you attempt with the procedural style : $date = date_create(); $myentity->setTimestamp(date_timestamp_get($date)); have you any satisfied result ?

    See DateTime::format

  2. You can also attempt with $date = new DateTime(); $myentity->setTimestamp($date->format('U')); // return seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)

    See time()