-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
GromNaN
merged 11 commits into
mongodb:5.x
from
saineshmamgain:fix/error-accessing-carbon-immutable-property
Apr 10, 2025
+74
−2
Merged
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
b751c59
Fixe: error when accessing a immutable_datetime casted property when …
saineshmamgain 0534989
Fix: test
saineshmamgain dc67388
Fix: test
saineshmamgain a7e288f
Fix: test
saineshmamgain 4e03bb7
Merge remote-tracking branch 'origin/fix/error-accessing-carbon-immut…
saineshmamgain 8ea6d5c
Merge remote-tracking branch 'origin/fix/error-accessing-carbon-immut…
saineshmamgain 937670d
Merge remote-tracking branch 'origin/fix/error-accessing-carbon-immut…
saineshmamgain 8e534f0
Fix:test
saineshmamgain 5d3fb4d
Reset Date facade to use default in the tearDown method
saineshmamgain fd47edf
Fix: add call to `parent::tearDown()`
saineshmamgain bbee5c2
Fix CS
GromNaN File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MongoDB\Laravel\Tests\Eloquent; | ||
|
||
use Carbon\CarbonImmutable; | ||
use Illuminate\Support\Facades\Date; | ||
use MongoDB\Laravel\Tests\Models\Anniversary; | ||
use MongoDB\Laravel\Tests\TestCase; | ||
|
||
use function assert; | ||
|
||
final class DateTimeImmutableTest extends TestCase | ||
{ | ||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
Anniversary::truncate(); | ||
} | ||
|
||
protected function tearDown(): void | ||
{ | ||
Date::useDefault(); | ||
|
||
parent::tearDown(); | ||
} | ||
|
||
public function testCanReturnCarbonImmutableObject(): void | ||
{ | ||
Date::use(CarbonImmutable::class); | ||
|
||
Anniversary::create([ | ||
'name' => 'John', | ||
'anniversary' => new CarbonImmutable('2020-01-01 00:00:00'), | ||
]); | ||
|
||
$anniversary = Anniversary::sole(); | ||
assert($anniversary instanceof Anniversary); | ||
self::assertInstanceOf(CarbonImmutable::class, $anniversary->anniversary); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MongoDB\Laravel\Tests\Models; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
use MongoDB\Laravel\Eloquent\DocumentModel; | ||
use MongoDB\Laravel\Eloquent\Model as Eloquent; | ||
use MongoDB\Laravel\Query\Builder; | ||
|
||
/** | ||
* @property string $name | ||
* @property string $anniversary | ||
* @mixin Eloquent | ||
* @method static Builder create(...$values) | ||
* @method static Builder truncate() | ||
* @method static Eloquent sole(...$parameters) | ||
*/ | ||
class Anniversary extends Model | ||
{ | ||
use DocumentModel; | ||
|
||
protected $keyType = 'string'; | ||
protected $connection = 'mongodb'; | ||
protected $table = 'anniversary'; | ||
protected $fillable = ['name', 'anniversary']; | ||
|
||
protected $casts = ['anniversary' => 'immutable_datetime']; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
These annotations are interesting, I think we should document this practice (something I will ask the doc team).
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.
credit goes to @Treggats. I followed his
HiddenAnimal
model.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.
Happy to help :)
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.
@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 theBuilder
return type of a relation.A little gotcha is that sometimes you need a additional docblock on the variable. Like this.
It then knows about the properties, relations, etc.
Hope this helps.
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.
Thanks, I'm tracking this to work on it later PHPORM-316