[5.4] Use setRawAttributes in Pivot constructor to avoid casting twice #18149
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.
At present, the Pivot constructor uses
forceFill
to set the attributes of the pivot model, which usessetAttribute
to set each property and ends up casting the data provided as a string given by the database to the relevant type. Most notably, for attributes that are set to be cast as a JSON castable type (array, object etc.) this results in a JSON encoded string from the database being JSON encoded again when it's being set in the Pivot model. Therefore, accessing such an attribute results in a JSON encoded string being returned rather than an array or stdClass object as expected.This pull request changes
forceFill
tosetRawAttributes
so that casting is not carried out when setting the Pivot attributes.#18135
Note: This PR is causing 2 tests to fail. One which ensures mutators are called from the constructor of Pivot and another which ensures that a dummy date passed in has been mutated. My suggestion intentionally skips the mutators by using
setRawAttributes
, similar to when a Model is created, therefore I'm not sure what to do regarding this.