Symfony2 Timestable trait: "Column 'createdAt' cannot be null"
doctrine-orm, php, symfony, traits
Solution
You have to set the values within the class manually. Then you can tell doctrine to set the new value before every update:
public function __construct() {
$this->setCreatedAt(new \DateTime());
$this->setUpdatedAt(new \DateTime());
}
/**
* @ORM\PreUpdate
*/
public function setUpdatedAtValue() {
$this->setUpdatedAt(new \DateTime());
}
Problem
I have a pretty standard Entity with the correct imports: ``` /** * Budhaz\aMailerBundle\Entity\Instance * * @ORM\Table() * @ORM\Entity */ class Instance { use TimestampableEntity; /** @ORM\Id @ORM\GeneratedValue @ORM\Column(type="integer") */ private $id; ... } ``` But I would like to remove the createdAt (and updatedAt) from my form so the user don't and can't set them, so I remove it from the 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') ; } ... } ``` But now I have this error: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'createdAt' cannot be null createdAt and updatedAt should automaticaly be set by Doctrine, but it stays null, anyone know why?