-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Fix casting issue #2653
Fix casting issue #2653
Conversation
This comment was marked as off-topic.
This comment was marked as off-topic.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for reviewing these issues and proposing a fix. I have a few remarks, and I still need to test your branch. But overall it looks great!
Big up for all the tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you think there is some error cases that could be added? Like data that can’t be casted.
protected $casts = [ | ||
'intNumber' => 'int', | ||
'floatNumber' => 'float', | ||
'decimalNumber' => 'decimal:2', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a case with decimal
without the prevision part?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As mentioned in the #2146 issue, an error will be thrown.
assertEquals replaced with assertSame;
This reverts commit 64ef154.
I tried my best to cover any possible failure in casting values and nothing new came to my mind. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM.
Thank you @hans-thomas. I merged into 4.1 because the behavior changes and some users may rely on the current behavior. |
Thanks @hans-thomas for this! It works, but not in all cases. When using date casts, you can also define the format in the string format, like this (docs): protected $casts = [
'mydate' => 'datetime:j.n.Y H:i',
]; When using cast like that, it doesn't work. Without formatting set, it does work as expected: protected $casts = [
'mydate' => 'datetime',
]; |
Hello @apeisa, I had some tests on |
Sure thing, here you are #2655 |
@GromNaN: Did you create a PHPORM issue to track this? If not, is it worth doing so for generating release notes down the line, or is that not necessary for this project? |
Coming back to this after some time away.
Because it doesn't and I'm on v4.3.0. |
Hello friends,
If you had a look at this repository, you would find that one of the most reported issues is failing to cast attributes on creating/saving data on the database. There are some issues that have been opened since 2018: #1580, #2199, #2257, #2393, #2651.
I applied #2257 PR and specifically wrote tests for each data type that might had an impact.
In this PR, I had to override
asDecimal
andfromJson
methods from theIlluminate\Database\Eloquent\Concerns\HasAttributes
trait.