Doctrine2试图将DateTime(“now”)插入mysql datetime列

时间:2012-09-14 13:11:55

标签: php doctrine-orm

我试图通过使用以下代码在mysql datetime列中插入日期时间但是没有插入。虽然isDelete工作正常。

/*
 * 
 * @ORM\Column (type="datetime")
 */
protected $created;   

 /** 
 * @ORM\PrePersist
 */
public function prePersist(){
    $this->created = new \DateTime("now"); 
    $this->isDelete = 0;        
}

生成的架构:

+-----------+------------+------+-----+---------+----------------+
| Field     | Type       | Null | Key | Default | Extra          |
+-----------+------------+------+-----+---------+----------------+
| id        | int(11)    | NO   | PRI |         | auto_increment |
| isDeleted | tinyint(4) | YES  |     | 0       |                |
| created   | datetime   | YES  |     |         |                |
+-----------+------------+------+-----+---------+----------------+

有什么想法吗?

Reference

2 个答案:

答案 0 :(得分:2)

您在*属性上方的/*之后错过了protected $created,因此它不是docblock注释,而且ergo未被解析为注释。因此,Doctrine不承认该领域。

应该是:

/**
 * 
 * @ORM\Column (type="datetime")
 */
protected $created; 

解决此问题后,运行Doctrine架构更新工具。

答案 1 :(得分:1)

你的班级顶部有* @ORM\HasLifecycleCallbacks()吗?

可能删除似乎有效,但您只能看到整数字段(0)的默认值