[8.x] Fixes AsEncrypted
traits not respecting nullable columns
#39848
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.
What?
When using
AsEncryptedCollection
orAsEncryptedArrayObject
, nulling the property of the model results in an error because the cast object returned isn't aware ofnull
values being passed to or received from the database.This fixes this behaviour by checking if the key in the attributes being passed to the cast object is not
null
. If it's not, proceeds to encrypt/decrypt the value, otherwise it keeps it asnull
.Notes
There is an EXCESSIVE amount of redundant encryption being done when setting a value into a property. For example, saving the model calls the encrypted 4 times, when it only should be called once.
You can check this behaviour on the tests themselves. The encryption must be set to be expected 12 times even if only it's saved twice in the database during a single test.