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

Fix nested dates format #2271

Closed
wants to merge 1 commit into from
Closed

Conversation

alexsmko
Copy link

@alexsmko alexsmko commented Jun 9, 2021

@divine
Copy link
Contributor

divine commented Jun 12, 2021

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) {
Copy link

@shaedrich shaedrich Jul 14, 2021

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:

Suggested change
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))
));
}
}

Choose a reason for hiding this comment

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

@GromNaN Does #3105 support the dot syntax?

Copy link
Member

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.

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

@GromNaN GromNaN added this to the 5.0 milestone Jul 22, 2024
@GromNaN
Copy link
Member

GromNaN commented Sep 3, 2024

Fixed by #3105

@GromNaN GromNaN closed this Sep 3, 2024
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