Reputation: 1991
Within the same entity I have a PreUpdate and a PrePersist. The PreUpdate fires, but the PrePersist never does. I put a die()
after the flush and comments within the lifecycle callbacks. Full entity can be seen at http://pastebin.com/yUk1u4GQ
Entity callbacks
/**
* @PreUpdate
*/
public function fixDates(){
$this->updatedOn = $this->getNow();
$this->closedDate = null;
$this->openDate = null;
print "dates fixed";
}
/**
* @PrePersist
*/
public function prePersist() {
print 'in prePersist';
die();
}
Entity Manager calls
$em->persist($school);
$em->flush();
die();
My screen reads "dates fixed", but not the prePersist message. I do have the @HasLifecycleCallbacks
at the top of the entity.
Upvotes: 27
Views: 17134
Reputation: 863
Maybe it version dependent but my working annotations have a next view:
Life cycle class annotation:
/**
* @Entity @Table(name="table_name")
* @HasLifecycleCallbacks
**/
Events annotations:
/** @PrePersist **/
/** @PreUpdate **/
That is all that i have in Model.
Upvotes: 5
Reputation: 167
I know this question is almost 2 years old, but I just had the exact same problem and since this doesn't have an accepted answer I want to share one last thing everyone else forgot to mention.
Although it seems that the triggered method will be used only by the entity class itself, it's scope should be kept public. My method wasn't triggering just because I marked it as a protected one. I hope this will help someone.
Upvotes: 11
Reputation: 885
Don't forget to enable Lifecycle Callbacks in your class annotation :
/**
* Report\MainBundle\Entity\Serveur
* @ORM\HasLifecycleCallbacks
*/
class Serveur {
Upvotes: 69
Reputation: 913
PrePersist is fired only when you are performing INSERT
statement, not UPDATE
statement.
When testing, don't forget that the UPDATE
statement is fired only when the entity attributes really change. If the Entity Manager is being called to persist that entity, it first looks if there are any changes. If not, no sql query is performed and no @PreUpdate
method is called.
Upvotes: 42
Reputation: 6529
I just had the same problem. Hope this helps you:
I forgot to import the annotations with the use statement. If you try this dont forget to add the "ORM" prefix:
use Doctrine\ORM\Mapping as ORM;
// ...
/**
* @ORM\PreUpdate
*/
public function preUpdate()
{
}
Upvotes: 6