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

/**
 * 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')
        ;
    }
...
}

但是现在我有这个错误:



Doctrine应该自动设置createdAt和updatedAt,但是它保持为空,有人知道为什么吗?

最佳答案

您必须在类中手动设置值。然后,您可以告诉教义在每次更新之前设置新值:

public function __construct() {
    $this->setCreatedAt(new \DateTime());
    $this->setUpdatedAt(new \DateTime());
}

/**
 * @ORM\PreUpdate
 */
public function setUpdatedAtValue() {
    $this->setUpdatedAt(new \DateTime());
}

关于php - Symfony2时间表特性: "Column ' createdAt' cannot be null",我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14956381/

10-11 03:07