-
-
Notifications
You must be signed in to change notification settings - Fork 249
Implement the compare method on the eloquent casts to improve the isDirty check #1033
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
Implement the compare method on the eloquent casts to improve the isDirty check #1033
Conversation
It looks like the Illuminate\Contracts\Database\Eloquent\ComparesCastableAttributes interface might be missing from your implementation. |
This is on purpose, If the ComparesCastableAttributes interface would be implement it would mean that the minimal required laravel version becomes 12.18.0 or higher for this package, while the current version contraint is By not implementing it, we can be sure that that everything is backward compatible. Laravel doesn't check if the interface is implemented, but does a simple check if the method exists on the cast class itself. So the fix would work for 12.18 and higher while staying compatible with earlier versions. |
Ah okay, I hadn't considered the backward compatibility aspect — that makes perfect sense. Smart solution! I just noticed two small things:
Maybe the comparison could be improved like this: return $this->get($model, $key, $firstValue, [])?->toArray() === $this->get($model, $key, $secondValue, [])?->toArray(); |
a11de4c
to
a7d3cbf
Compare
I updated the method so that it uses the For the nullsafe operator, I noticed that the method
I added this case into the unit test, but to play "safe" I could add the nullsafe operator anyway. Furthermore I updated the typehints in |
a7d3cbf
to
b8cf0d1
Compare
Looking good, could you please take look at the failing tests? If that's done this one can be merged. Note for myself quickly cleanup a few parts of the tests. |
@rubenvanassche Done, I added a check on the laravel version so that the tests are skipped for older versions.
Nvm, it also uses |
Thanks! |
Hi @rubenvanassche @SanderSander From last version we have some erros with this implementation. We are using nullable dtos, so the method toArray() dont apply to this. Maybe add the nullsafe operator, which means if an attribute is nullable and its value changes from null to a non-null object. @clementbirkle made the same comment |
Isn't this the case?
Otherwise feel free to PR it |
Hey, can you give me an example, so that I can write a test for your use case? I have covered the use case with null values, see the following test https://github.com/spatie/laravel-data/pull/1033/files#diff-f7aacc43a3f446f7d13176dbaef1985b860b5fed2e78e4905359c6eb4baefe5bR262 So maybe i missed something, but would like a bit more information because I'm not completely sure how to re-create |
https://order-editing.sentry.io/share/issue/727b1319eb03410796778ec30cc4df0f/ this is what we're getting. This is going from it being null to non-null. |
@nick-potts Thanks! I'm making a PR to fix this issue |
We are experiencing the same issue
There is a null check for $attribute but not for $original. Link for PR -> #1046 |
Resolves #1032