Skip to content
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

Datetime casting with custom format #2658

Merged
merged 24 commits into from
Nov 8, 2023
Merged

Datetime casting with custom format #2658

merged 24 commits into from
Nov 8, 2023

Conversation

hans-thomas
Copy link
Contributor

@hans-thomas hans-thomas commented Oct 31, 2023

Hi again,
As mentioned in the #2655 issue, casting date types with a custom format did not work properly. So I decided to cover all date types and fix this issue.
I tested these date types:

  • date with custom formatting
  • immutable_date
  • immutable_date with custom formatting
  • datetime
  • immutable_datetime
  • immutable_datetime with custom formatting

To achieve this goal, I override one of the cases in castAttribute method to handle immutable_date date type. Also, I override transformModelValue method to check if the result is a DateTimeInterface or not for applying defined custom formatting on the Carbon instance and resetting time in a custom date type.
In the end, I should say that all date values will be stored in DB as Date type. (not as an object or string anymore)

@hans-thomas hans-thomas marked this pull request as ready for review October 31, 2023 14:46
@hans-thomas
Copy link
Contributor Author

I have no idea why pipeline is failing.

@hans-thomas hans-thomas marked this pull request as draft October 31, 2023 14:48
Copy link
Member

@GromNaN GromNaN left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a good start

src/Eloquent/Model.php Show resolved Hide resolved
src/Eloquent/Model.php Outdated Show resolved Hide resolved
src/Eloquent/Model.php Outdated Show resolved Hide resolved
@hans-thomas hans-thomas marked this pull request as ready for review November 2, 2023 12:18
@GromNaN GromNaN added this to the 4.1 milestone Nov 2, 2023
@mongodb mongodb deleted a comment from codecov-commenter Nov 2, 2023
@mongodb mongodb deleted a comment from codecov-commenter Nov 2, 2023
src/Eloquent/Model.php Outdated Show resolved Hide resolved
@GromNaN GromNaN requested a review from alcaeus November 2, 2023 20:24
@codecov-commenter

This comment was marked as outdated.

@GromNaN GromNaN requested a review from jmikola November 2, 2023 20:34
@GromNaN GromNaN merged commit 899a235 into mongodb:4.1 Nov 8, 2023
13 checks passed
@GromNaN
Copy link
Member

GromNaN commented Nov 8, 2023

Thank you @hans-thomas

@hans-thomas
Copy link
Contributor Author

@GromNaN Always a Pleasure❤️
Could you please close the mentioned issue?

@hans-thomas hans-thomas deleted the 2655 branch November 23, 2023 13:13
GromNaN added a commit that referenced this pull request Mar 5, 2024
…ield with custom format (#2741)

- Fix #2729 by removing the method `Model::castAttribute()` that was introduced by #2658 in 4.1.0. Tests added to ensure `null` is allowed in `date`, `datetime`, `immutable_date`, `immutable_datetime` and the variants with custom formats.
- Change the behavior of `immutable_date:j.n.Y H:i` (with custom format), to reset the time. This behave differently than [Laravel 10.46 that treats it like a `immutable_datetime`](https://github.com/laravel/framework/blob/v10.46.0/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php#L866-L869).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants