5
votes

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?

3
Show please annotations for createdAt and updatedAt fields in Entity classMaksim Ustinov
I don't have them because it's a trait.Dorian

3 Answers

11
votes

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());
}
10
votes

app/config/config.yml

stof_doctrine_extensions:
   orm:
      default:
          timestampable: true
2
votes

You need to check if the bundle is installed. If not, run: composer require stof/doctrine-extensions-bundle

It should create file: app/config/packages/stof_doctrine_extensions.yaml with the content:

stof_doctrine_extensions:
    default_locale: en_US
    orm:
        default:
            timestampable: true