prePersist() method of my entity is not being invoked.
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 had the same issue, it was because my method was private, so make sure the method with @ORM\PrePersist()
annotation is public.
@ORM\PrePersist
is missing parenthesis, it should be @ORM\PrePersist()
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();
}
I solved this issue cleaning the cache!
php bin/console cache:clear