我有一个带有正确导入的非常标准的实体:
/**
* Budhaz\aMailerBundle\Entity\Instance
*
* @ORM\Table()
* @ORM\Entity
*/
class Instance {
use TimestampableEntity;
/** @ORM\Id @ORM\GeneratedValue @ORM\Column(type="integer") */
private $id;
...
}
但我想从我的表单中删除 createdAt(和 updatedAt),这样用户就不会也不能设置它们,所以我将它从 InstanceForm 中删除:
class InstanceType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name')
->add('startAt')
->add('endAt')
//->add('createdAt')
//->add('updatedAt')
->add('campaign')
;
}
...
}
但是现在我有这个错误:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'createdAt' cannot be null
createdAt 和 updatedAt 应该由 Doctrine 自动设置,但它保持为空,有人知道为什么吗?
最佳答案
您必须手动设置类中的值。然后你可以告诉 doctrine 在每次更新之前设置新值:
public function __construct() {
$this->setCreatedAt(new \DateTime());
$this->setUpdatedAt(new \DateTime());
}
/**
* @ORM\PreUpdate
*/
public function setUpdatedAtValue() {
$this->setUpdatedAt(new \DateTime());
}
关于php - Symfony2 Timestable 特征 : "Column ' createdAt' cannot be null",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14956381/