Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

preFlush event and lifecycle callback #169

Merged
merged 4 commits into from
Nov 13, 2011
Merged

preFlush event and lifecycle callback #169

merged 4 commits into from
Nov 13, 2011

Conversation

everzet
Copy link
Contributor

@everzet 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.

*/
private $_em;

//private $_entitiesToPersist = array();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this code still needed ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, missed that :-) Fixed!

@beberlei
Copy link
Member

What about XML and YML?

@everzet
Copy link
Contributor Author

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 :-)

@@ -259,6 +259,41 @@ class UnitOfWork implements PropertyChangedListener
*/
public function commit($entity = null)
{
// Run preFlush lifecycle callback for new entities
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed in commit below ;-)

@beberlei
Copy link
Member

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?

@everzet
Copy link
Contributor Author

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
@beberlei
Copy link
Member

Merged

@alexndlm alexndlm mentioned this pull request Oct 5, 2016
yvoyer pushed 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
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants