2
我使用的是ZF2和Doctrine-Module的Doctrine 2。 我已經寫了一個實體,需要一個更新前| PrePersist,因爲教義不允許 日期|日期時間在主鍵:Doctrine HasLifecycleCallbacks PrePersist | PreUpdate不會觸發
<?php
namespace Application\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
*
* @ORM\Entity
* @ORM\HasLifecycleCallbacks
* @ORM\Table(name="sample")
*/
class Sample
{
/**
*
* @ORM\Id
* @ORM\Column(type="string")
* @var integer
*/
protected $runmode;
/**
*
* @ORM\Id
* @ORM\Column(type="date")
* @var DateTime
*/
protected $date;
public function getRunmode()
{
return $this->runmode;
}
public function setRunmode($runmode)
{
$this->runmode = $runmode;
return $this;
}
public function getDate()
{
return $this->date;
}
public function setDate($date)
{
$this->date = $date;
return $this;
}
/**
*
* @ORM\PreUpdate
* @ORM\PrePersist
*/
protected function formatDate()
{
die('preUpdate, prePersist');
if ($this->date instanceof \DateTime) {
$this->date = $this->date->format('Y-m-d');
}
return $this;
}
}
現在的問題,如果我設置日期時間爲日期我得到消息:
"Object of class DateTime could not be converted to string"
因爲它不走進formatDate。
在我的情況下,ORM:PrePersist被忽略了......你註冊了一些服務還是其他的東西? – Ron 2013-06-03 12:01:39
@它是一個回調。它必須是公開的,並且必須在元數據中註冊。 – Ocramius 2013-06-03 12:53:44
謝謝!但我如何在元數據中註冊它?從來沒有見過這個......或者它可能是trival? – Ron 2013-06-03 13:45:06