Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Fixes: getting immutable_datetime property fails if Date::use(CarbonImmutable::class) is set #3342

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

Merged

Conversation

@saineshmamgain
Copy link
Contributor

@saineshmamgain saineshmamgain commented Apr 9, 2025

This PR fixes the issue #3341 .

Basically when Date::use(CarbonImmutable::class); is added. The method asDateTime in DocumentModel trait throws an exception. I've changed the return type of asDateTime method from Carbon to DateTimeInterface.

Checklist

  • Add tests and ensure they pass

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.

I added a minor comment, but the overall seems good to me. Thanks!

* @mixin Eloquent
* @method static Builder create(...$values)
* @method static Builder truncate()
* @method static Eloquent sole(...$parameters)
Copy link
Member

Choose a reason for hiding this comment

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

These annotations are interesting, I think we should document this practice (something I will ask the doc team).

Copy link
Contributor Author

@saineshmamgain saineshmamgain Apr 10, 2025

Choose a reason for hiding this comment

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

credit goes to @Treggats. I followed his HiddenAnimal model.

Treggats reacted with hooray emoji
Copy link
Contributor

Choose a reason for hiding this comment

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

Happy to help :)

saineshmamgain reacted with rocket emoji
Copy link
Contributor

Choose a reason for hiding this comment

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

@GromNaN the thing with Laravel Models is that by default they have the type Model. Which makes sense, as the model extends that class, but then the IDE doesn't know about any of the methods, relations, properties, etc.

These docblocks provide the IDE with the context it should have. Note; the @method docblock can not be used if the method already exists in the class.

Personally I also use @property for the columns of the database table and the relation once fetched.
The @property-read docblock is used for the Builder return type of a relation.

A little gotcha is that sometimes you need a additional docblock on the variable. Like this.

use Tests\Models\Anniversary;
/** @var Anniversary $anniversary */
$anniversary = Anniversary::query()->where('name', 'John')->first();

It then knows about the properties, relations, etc.

Hope this helps.

Copy link
Member

Choose a reason for hiding this comment

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

Thanks, I'm tracking this to work on it later PHPORM-316

Copy link
Member

GromNaN commented Apr 10, 2025

@saineshmamgain can you look at the test failure:

1) MongoDB\Laravel\Tests\Eloquent\DateTimeImmutableTest::testCanReturnCarbonImmutableObject
* Test code or tested code did not remove its own error handlers
* Test code or tested code did not remove its own exception handlers
/home/runner/work/laravel-mongodb/laravel-mongodb/tests/DateTimeImmutableTest.php:28

Copy link
Contributor Author

@GromNaN actually this error comes when I do the reset Date::useDefault(); in tearDown Method.

Copy link
Contributor Author

@GromNaN My bad missed call to parent::tearDown() in my method. Updated the PR.

GromNaN reacted with thumbs up emoji

@GromNaN GromNaN merged commit 8b5ab59 into mongodb:5.x Apr 10, 2025
70 checks passed
Copy link
Member

GromNaN commented Apr 10, 2025

Thank you @saineshmamgain

saineshmamgain reacted with hooray emoji

@GromNaN GromNaN added this to the 5.3 milestone Apr 10, 2025
@saineshmamgain saineshmamgain deleted the fix/error-accessing-carbon-immutable-property branch April 11, 2025 04:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@GromNaN GromNaN GromNaN approved these changes

@jmikola jmikola Awaiting requested review from jmikola jmikola is a code owner automatically assigned from mongodb/dbx-php

+1 more reviewer

@Treggats Treggats Treggats left review comments

Reviewers whose approvals may not affect merge requirements

Assignees

No one assigned

Labels

Projects

None yet

Milestone

5.3

Development

Successfully merging this pull request may close these issues.

AltStyle によって変換されたページ (->オリジナル) /