Symfony2 ORM prePersist Not Working Symfony2 ORM prePersist Not Working symfony symfony

Symfony2 ORM prePersist Not Working


I have the same problem. According to docs you must enable lifecycle callbacks http://symfony.com/doc/current/book/doctrine.html

/** * @ORM\Entity() * @ORM\HasLifecycleCallbacks() */class Product{    // ...}


I solved this issue cleaning the cache!

php bin/console cache:clear


One issue is this function.

/** * Set active * * @ORM\PrePersist * @param boolean $active * @return Users */public function setActive($active){    $this->active = $active;    return $this;}

You can't pass arguments to functions when using @PrePersist.

Also this function

/*** @ORM\PrePersist* @ORM\PreUpdate*/public function prePersist(){  $this->active = 0;  $this->created = date('Y-m-d H:i:s');  $this->modified = date('Y-m-d H:i:s');}

fails because you are assigning a string value to DateTime fields. Try this:

/** * @ORM\PrePersist * @ORM\PreUpdate */public function prePersist(){  $this->active = false;  $this->created = new \DateTime();  $this->modified = new \DateTime();}