Add a unique event ID so we can match pre/post Insert/Update #19209
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Overview
There are two ways to listen for changes to entities via pre/post. Old method via the hook_civicrm_pre/post hooks or new/standardised symfony events
civi.dao.preInsert
/civi.dao.postInsert
/civi.dao.preUpdate
/civi.dao.postUpdate
There is no way to link a preInsert/Update event with a postInsert/Update event for the same entity/change. Normally this doesn't matter but for a "busy" site or where there are multiple things happening based on a change to an entity this is problematic.
This came up specifically on a site with:
This resulted in inconsistent behaviour which we tracked down to timing issues. Sometimes the pre-hook fired for both 1 and 2 BEFORE the post hook had fired for the relevant change. Resulting in the "original data" being overwritten by the second pre-hook and not matching the change that was being checked in the post hook.
I've come across similar "challenges" before and being able to match up the pre/post for the same change would make things much easier. With the work that @colemanw has done for API4/standardisation of the DAO this is now possible in just 5 lines of code.
Before
Not possible to link a pre/post Insert/Update event for the same change.
After
Can use the eventID to link a pre/post Insert/Update event for the same change.
Technical Details
I used
uniqID()
but it really doesn't matter too much as long as the ID is unique in the context of the events firing at the time.I added
eventID
as a property to the event separate to the entity params.Comments
@colemanw @seamuslee001 @eileenmcnaughton What do you think? This seems like a nice simple solution to a problem that's come up for me multiple times and most likely has for others too!