Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 Timestable trait: "Column 'createdAt' cannot be null"

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?

like image 465
Dorian Avatar asked Feb 19 '13 11:02

Dorian


1 Answers

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());
}
like image 55
insertusernamehere Avatar answered Nov 03 '22 15:11

insertusernamehere