所以,我的实体中有一个像这样的 DateTime 字段:
/**
* @var \DateTime
*
* @ORM\Column(name="timestamp", type="datetime")
*/
private $timestamp;
每次我在数据库中插入一些东西时,我都会这样做:
$myEntity->setTimestamp(new \DateTime('now'));
所以,我想在我的实体中的字段上设置默认值,但是当我尝试时:
/**
* @var \DateTime
*
* @ORM\Column(name="timestamp", type="datetime")
*/
private $timestamp = new \DateTime('now');
然后用
doctrine:schema:update
更新我的基地我有这个错误:PHP Parse error: syntax error, unexpected 'new' (T_NEW) in...
如何为该字段设置默认值?我不想每次使用我的实体时都设置时间戳...谢谢!
最佳答案
你在构造函数中做。
public function __construct(){
$this->timestamp(new \DateTime());
}
关于php - Symfony2,设置日期时间字段的默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22529226/