我有父实体类:
namespace App\Model\Entities;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\MappedSuperclass
*/
abstract class ParentEntity
{
/**
* @var int|null
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue
*/
protected $id;
/**
* @ORM\Column(type="datetime", columnDefinition="DATETIME on update CURRENT_TIMESTAMP")
*/
protected $modifiedOn;
}
并且每次我运行php bin/console o:s:u --dump-sql
或--force
时,控制台都会打印出将执行X个查询,并且每个查询都针对modified_on
列。
ALTER TABLE contact CHANGE modified_on modified_on DATETIME on update CURRENT_TIMESTAMP;
ALTER TABLE about_us CHANGE modified_on modified_on DATETIME on update CURRENT_TIMESTAMP;
ALTER TABLE image CHANGE modified_on modified_on DATETIME on update CURRENT_TIMESTAMP;
...
在MySQL数据库中,所有设置均正确设置:
CREATE TABLE `about_us` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`modified_on` datetime DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
所以我的问题是为什么会这样?我是否错过了教义的某些配置/注释?还是必须以其他方式指定此属性?
答案 0 :(得分:2)
您可能在Lifecycle Callbacks之后。如果是这样,这就是方法:
namespace App\Model\Entities;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\MappedSuperclass
* @ORM\HasLifecycleCallbacks
*/
abstract class ParentEntity
{
/**
* @var int|null
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue
*/
protected $id;
/**
* @ORM\Column(type="datetime")
*/
protected $modifiedOn;
/**
* @ORM\PreUpdate
*/
public function setModifiedOn()
{
$this->modifiedOn = new \DateTime();
}
}