Symfony 条令在更新当前时间戳时保持更新日期时间
我有父实体类:Symfony 条令在更新当前时间戳时保持更新日期时间,symfony,doctrine-orm,annotations,doctrine,Symfony,Doctrine Orm,Annotations,Doctrine,我有父实体类: 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 */
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个查询将被执行,并且每个查询都是针对列上的修改的
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;
所以我的问题是为什么会发生这种情况?我是否错过了一些条令的配置/注释?或者,您必须以其他方式指定此属性吗?您可能需要。如果是这样的话,这就是出路:
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();
}
}