[8.x] Fire pivot detach events when using custom class #36300
Closed
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.
Fixes #36291
This adjusts the detach logic when using custom classes such that the deleting and deleted events are fired for the pivot models. This also adjusts new pivot queries to wrap their conditions in a logical group for a lesser chance of unexpected query results.
The events are fired by retrieving the pivot models and then maintaining the same logic where we rehydrate new empty models. I tried to keep the logic as close as possible to the original implementation.
The decision to not fire these events was done here: #27997 - but I believe that was more of a "quick fix" to stop a breakage when pivot events were added. The original implementation did not carry over any additional where clauses when using a custom pivot class and thus caused the bug where all pivot records were deleted. However, this PR fixes that by carrying over the where clauses.
Also, in the meantime since the original PR, @themsaid fixed an issue with updating existing pivots (#28416) ... this also carries over where clauses. So, I don't think we need to check for empty where clauses before using
updateExistingPivotUsingCustomClass
.