php - Symfony2 Timestable 特征 : "Column ' createdAt' cannot be null"

标签 php symfony doctrine-orm traits

我有一个带有正确导入的非常标准的实体:

/**
 * 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/

相关文章:

symfony - Doctrine 中的多态单向关系就像 Eloquent 一样?

javascript - 为什么 Wordpress ajax 会死 ('0' )

php - 允许空格的正则表达式(对于电话号码正则表达式)

php - Mysql查询逗号分隔过滤结果

php - XML 到 MySql 数据库 (SimpleXML)

symfony - 为什么 Symfony 3.3.13 源代码会生成弃用警告(不在我的代码中)?

php - Symfony2 DOMCrawler selectLink 返回 null uri

css - ERR_ABORTED 404(未找到)CSS 样式表 Symfony

php - Doctrine Repository getEntityManager() 确实返回空

php - 实体关系