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

PHPORM-148 Fix null in datetime fields and reset time in date field with custom format #2741

Merged
merged 5 commits into from
Mar 5, 2024

Conversation

GromNaN
Copy link
Member

@GromNaN GromNaN commented Mar 1, 2024

Fix PHPORM-148
Fix #2729

Checklist

  • Add tests and ensure they pass
  • Add an entry to the CHANGELOG.md file
  • Update documentation for new features

@GromNaN GromNaN requested review from jmikola and alcaeus March 1, 2024 18:31
@GromNaN GromNaN requested a review from a team as a code owner March 1, 2024 18:31
$castType = $this->getCasts()[$key];
if ($this->isCustomDateTimeCast($castType) && str_starts_with($castType, 'date:')) {
$value->startOfDay();
if (str_starts_with($castType, 'date:') || str_starts_with($castType, 'immutable_date:')) {
Copy link
Member Author

Choose a reason for hiding this comment

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

The is no method to check a "date" cast, as opposed to "datetime" cast that keep the time.

Comment on lines -324 to -326
'immutable_custom_datetime','immutable_datetime' => str_starts_with($this->getCasts()[$key], 'immutable_date:') ?
$this->asDate($value)->toImmutable() :
$this->asDateTime($value)->toImmutable(),
Copy link
Member Author

Choose a reason for hiding this comment

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

The cast types immutable_custom_datetime and immutable_datetime are already supported by the parent method: HasAttribute::castAttribute()

Copy link
Member

Choose a reason for hiding this comment

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

How does $this->getCastType($key) differ from $this->getCasts()[$key]? I don't understand why this would match on immutable_custom_datetime or immutable_datetime but then see a immutable_date: prefix from the latter method.

I did note that the function you linked already handles:

case 'date':
    return $this->asDate($value);
...
case 'immutable_date':
    return $this->asDate($value)->toImmutable();

Copy link
Member Author

Choose a reason for hiding this comment

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

  • $this->getCasts()[$key] is what is defined in the $casts property (date:j.n.Y H:i).
  • $this->getCastType($key) is a classification (custom_datetime)

if ($this->isCustomDateTimeCast($castType) && str_starts_with($castType, 'date:')) {
$value->startOfDay();
if (str_starts_with($castType, 'date:') || str_starts_with($castType, 'immutable_date:')) {
$value = $value->startOfDay();
Copy link
Member Author

Choose a reason for hiding this comment

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

$value is reassigned because of immutable object.

@GromNaN
Copy link
Member Author

GromNaN commented Mar 4, 2024

I'm working on this PR, it creates new issues with Laravel 11 (branch 4.2). (wrong test)

Comment on lines -324 to -326
'immutable_custom_datetime','immutable_datetime' => str_starts_with($this->getCasts()[$key], 'immutable_date:') ?
$this->asDate($value)->toImmutable() :
$this->asDateTime($value)->toImmutable(),
Copy link
Member

Choose a reason for hiding this comment

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

How does $this->getCastType($key) differ from $this->getCasts()[$key]? I don't understand why this would match on immutable_custom_datetime or immutable_datetime but then see a immutable_date: prefix from the latter method.

I did note that the function you linked already handles:

case 'date':
    return $this->asDate($value);
...
case 'immutable_date':
    return $this->asDate($value)->toImmutable();

@@ -315,19 +316,6 @@ protected function fromDecimal($value, $decimals)
return new Decimal128($this->asDecimal($value, $decimals));
}

/** @inheritdoc */
protected function castAttribute($key, $value)
Copy link
Member

Choose a reason for hiding this comment

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

Is the removal of this method what actually fixes the null-handling issue cited in PHPORM-148?

Copy link
Member Author

Choose a reason for hiding this comment

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

The actual fix is more in transformModelValue. It seemed to me that this code was an incomplete attempt to remove the time from immutable_date with custom format; which failed with null values.

@GromNaN GromNaN requested a review from jmikola March 4, 2024 18:28
Copy link
Member

@jmikola jmikola left a comment

Choose a reason for hiding this comment

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

LGTM, although I don't fully grasp the moving parts here.

I'm satisfied with the test cases covering the original reproduction in PHPORM-148.

CHANGELOG.md Outdated Show resolved Hide resolved
Co-authored-by: Andreas Braun <git@alcaeus.org>
@GromNaN GromNaN merged commit 87741d7 into mongodb:4.1 Mar 5, 2024
15 checks passed
@GromNaN GromNaN deleted the PHPORM-148 branch March 5, 2024 08:49
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.

3 participants