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

Avoid resaving element in Elements::EVENT_AFTER_SAVE_ELEMENT event handler #75

Closed

Conversation

mmikkel
Copy link
Collaborator

@mmikkel mmikkel commented May 18, 2021

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 the EVENT_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

@jalendport
Copy link
Owner

jalendport commented Jul 2, 2021

I was able to resolve this via 861741c in a much simpler way (which @mmikkel and I both agreed in a DM was probably the better solution at least temporarily). I would still be curious to hear from @brandonkelly on whether the changes in this PR are preferable over just switching to the propagation event. Gonna close this for now but will definitely reopen if this is a better long term solution 👍

@jalendport jalendport closed this Jul 2, 2021
@brandonkelly
Copy link
Contributor

EVENT_AFTER_PROPAGATE is probably a safer place to be, yes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants