Avoid resaving element in Elements::EVENT_AFTER_SAVE_ELEMENT event handler #75
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.
This PR rolls back the changes made in #66, which attempted to fix issues with Matrix blocks being deleted in certain cases. #66 solves this by re-fetching the newly saved element from the database inside the
Elements::EVENT_AFTER_SAVE_ELEMENT
event handler, but unfortunately this approach seems to have caused other issues, such as #71, #70, #73 and possibly #72.Rather than attempting to work around the problems introduced by #66, this PR takes a different approach to solving the original issue by updating the content table directly via the
Content::saveContent()
method, instead of re-saving the element itself. This appears to save the preparsed content properly after the element is saved, whilst keeping relational data (i.e. Matrix blocks) intact.I was able to reproduce both #71, #70 and #73 before applying this patch, and I'm not able to reproduce them after. I'm also unable to reproduce the original issue (#64, #66) with the patch applied. Additionally, I've tested that the patch works as intended on both multi-site and single-site setups, and when using the
resave
CLI command.A final note; I removed the
$element->setScenario()
call inside theEVENT_AFTER_SAVE_ELEMENT
event handler, simply because it should be redundant as the element is no longer re-saved.Disclaimer:
Unfortunately I'm not 100% confident that updating the content table directly, vs. actually re-saving the element, won't cause any unwanted side effects. As far as I've been able to test, it works (and, not resaving the element in the an
EVENT_AFTER_SAVE_ELEMENT
event handler also seems like a cleaner approach to me overall) but it could also be a Very Bad Idea. TL;DR, it might be good to get an actual expert's eyes on this PR before considering to merge. Maybe @brandonkelly has the time for a quick review? 😇Fixes #71
Fixes #70
Fixes #73