-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Fix nested dates format #2271
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Fix nested dates format #2271
Conversation
Hello, This looks like a breaking change for the projects depending on the old behavior, however, on the other hand, this is definitely a fix. @Smolevich what do you think? Thanks! |
@@ -216,7 +216,9 @@ public function attributesToArray() | |||
// Convert dot-notation dates. | |||
foreach ($this->getDates() as $key) { |
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.
Your change works for EmbedsOne
but not for EmbedsMany
since Arr::has()
/Arr::set()
doesn't support dot-syntax on nested arrays (entry.*.date
). Interestingly, array_get()
/array_set()
do. So I'd add this to make it work for both EmbedsOne
and EmbedsMany
:
foreach ($this->getDates() as $key) { | |
foreach ($this->getDates() as $key) { | |
$res = data_get($attributes, $key); | |
if (is_array($res)) { | |
$res = array_filter($res); | |
} | |
if (Str::contains($key, '.') && Arr::has($attributes, $key)) { | |
Arr::set($attributes, $key, $this->serializeDate( | |
$this->asDateTime(Arr::get($attributes, $key)) | |
)); | |
} else if (Str::contains($key, '.') && !empty($res)) { | |
data_set($attributes, $key, $this->serializeDate( | |
$this->asDateTime(Arr::get($attributes, $key)) | |
)); | |
} | |
} |
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.
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.
In fact, the query builder will convert any DateTimeInterface
into UTCDatetime
, and inversely for results. Regardless of the path, no need to specify any date casting. That the benefit of having a native date type in MongoDB vs SQL database that store strings for dates.
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.
Awesome 👍🏻 Thanks for the clarification
Fixed by #3105 |
#2129