-
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
Changes from 8 commits
b751c59
0534989
dc67388
a7e288f
4e03bb7
8ea6d5c
937670d
8e534f0
5d3fb4d
fd47edf
bbee5c2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?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(); | ||
} | ||
|
||
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); | ||
|
||
Date::useDefault(); | ||
} | ||
} |
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. credit goes to @Treggats. I followed his There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe 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 These docblocks provide the IDE with the context it should have. Note; the Personally I also use 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks, I'm tracking this to work on it later PHPORM-316 |
||
*/ | ||
class Anniversary extends Model | ||
{ | ||
use DocumentModel; | ||
|
||
protected $keyType = 'string'; | ||
protected $connection = 'mongodb'; | ||
protected $table = 'anniversary'; | ||
protected $fillable = ['name', 'anniversary']; | ||
|
||
protected $casts = ['anniversary' => 'immutable_datetime']; | ||
} |
Uh oh!
There was an error while loading. Please reload this page.