preFlush event and lifecycle callback #169

Merged
merged 4 commits into from Nov 13, 2011

Conversation

Projects
None yet
3 participants
Contributor

everzet commented Oct 23, 2011

For now, we have @PrePersist, @PreUpdate and @PreRemove callbacks. But it's not enough, cuz in some cases we need to run some entity method just before every EntityManager#flush() call (examples - translatable behavior and file uploading routines).

I've added and tested new @PreFlush event, which occurs during the start of the EntityManager#flush(), before any changeset gets calculated. This gives users ability to hook into flush process and prepare their entities to save even if they were not changed.

lib/Doctrine/ORM/Event/PreFlushEventArgs.php
+ */
+ private $_em;
+
+ //private $_entitiesToPersist = array();
@willdurand

willdurand Oct 23, 2011

Is this code still needed ?

@everzet

everzet Oct 23, 2011

Contributor

Yep, missed that :-) Fixed!

Owner

beberlei commented Oct 23, 2011

What about XML and YML?

Contributor

everzet commented Oct 23, 2011

@beberlei it should be evaluated in the same way:
https://github.com/doctrine/doctrine2/blob/master/lib/Doctrine/ORM/Mapping/Driver/YamlDriver.php#L456
https://github.com/doctrine/doctrine2/blob/master/lib/Doctrine/ORM/Mapping/Driver/XmlDriver.php#L443

There's no explicit event check in the Xml and Yaml driver. If the event name exists inside the Doctrine\ORM\Event class - doctrine just adds it to the class lifecycleCallbacks list, AFAIK :-)

lib/Doctrine/ORM/UnitOfWork.php
@@ -259,6 +259,41 @@ class UnitOfWork implements PropertyChangedListener
*/
public function commit($entity = null)
{
+ // Run preFlush lifecycle callback for new entities
@beberlei

beberlei Oct 24, 2011

Owner

couldnt triggering these be done inside computeChangeSets, before every specific entity? That way we only need to loop once.

@everzet

everzet Nov 3, 2011

Contributor

fixed in commit below ;-)

Owner

beberlei commented Nov 1, 2011

@everzet i think we dont need a lifecycle event for this. just an event for listeners. Why do you want this to be on the entities also?

Contributor

everzet commented Nov 3, 2011

@beberlei i've created this PR because of some specific tasks, like file uploading/reuploading and i18n management. For now, we only have way to hook into persist (happens one time) and update (happens only if entity is changed) events. But in many cases, user will want to prepare his entity just before every flush. For example, we need to record translation changes before every flush, even if entity itself haven't updated or we need to update image value in entity, even if entity itself haven't updated. This hook will add missing part of control into the user-defined domain objects. And if you don't need it - then just don't use it.

@beberlei beberlei merged commit 9c4c06c into doctrine:master Nov 13, 2011

Owner

beberlei commented Nov 13, 2011

Merged

alexndlm added a commit to Dreamlex/doctrine2 that referenced this pull request Oct 5, 2016

@alexndlm alexndlm referenced this pull request Oct 5, 2016

Merged

Update events.rst #6070

yvoyer added a commit to yvoyer/doctrine2 that referenced this pull request Nov 21, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment