Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony preUpdate vs prePersist

I am new to Symfony2 and I would like to know what is the difference in prePersist and preUpdate events. It looks like prePersist is 'fired' before I 'persist' a record, but when does preUpdate fire?

like image 843
mentalic Avatar asked Jan 15 '13 19:01

mentalic


3 Answers

Neither of these is part of Symfony specifically. They are part of Doctrine2. The prePersist fires at the point that an entity is first persisted. Persisting an object means that it is managed by the Doctrine entityManager, even though it does not actually get inserted into the database until a flush.

preUpdate is the corresponding event on an existing object that is about to be updated. Because an existing object is already managed by the entityManager at the point that it was queried, there is no equivalent persist event. It basically fires when an existing object has been changed, and a flush has been called.

In other words, if you didn't change anything in the object, PreUpdate will not run!

With that said, you can think of these as happening "just before insert" and "just before update".

There are also 2 forms of these: lifecycle callbacks, which can be annotated directly into the entity and added as methods inside of it, but only have access to the entity attributes. These can be good for simple manipulations like timestamping, conforming strings to a particular standard, or generating derived attributes.

There are also true event listeners which have to be registered with the entityManager, and have access to event data that has the type of before/after data you'd expect in a database trigger.

Note that in Doctrine version 2.4 they added event Data even for Lifecycle callbacks, which now makes it far simpler and easier to do the same sorts of things you previously needed to use event listeners for.

like image 91
gview Avatar answered Oct 21 '22 06:10

gview


Also worth noting:

-If your entity does not have any changed values, the PreUpdate will NOT trigger.

So you can't rely on this to just update a modification timestamp whenever a form is saved. This is particularly tricky if you have a collection of forms on one page, and the user may have updated some fields of included sub form collections. The entities that were updated, will trigger the PreUpdate, but the primary form entity will not trigger a PreUpdate unless it's OWN fields were updated.

-You can set multiple lifecycle callback annotations for both PrePersist and PreUpdate

So for example, if you want to set the modification time stamp when the record is created AND when it is updated, you can add both annotations to the same function in the entity, e.g.

/**
 * @ORM\PreUpdate
 * @ORM\PrePersist
 */
public function setTimeModValue() {
    $this->timeMod = time();
}
like image 19
Chadwick Meyer Avatar answered Oct 21 '22 04:10

Chadwick Meyer


From here: http://docs.doctrine-project.org/en/2.0.x/reference/events.html#lifecycle-events

prePersist - The prePersist event occurs for a given entity before the respective EntityManager persist operation for that entity is executed.

preUpdate - The preUpdate event occurs before the database update operations to entity data. It is not called for a DQL UPDATE statement.

Also, note that this is for Doctrine, not specifically Symfony.

like image 5
MDrollette Avatar answered Oct 21 '22 05:10

MDrollette