-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
PHPORM-239 Convert _id
and UTCDateTime
in results of Model::raw()
before hydratation
#3152
Changes from all commits
d8c1bb6
98df7c4
e743023
5a79bec
6763914
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 |
---|---|---|
|
@@ -28,6 +28,8 @@ | |
use MongoDB\Laravel\Tests\Models\Soft; | ||
use MongoDB\Laravel\Tests\Models\SqlUser; | ||
use MongoDB\Laravel\Tests\Models\User; | ||
use MongoDB\Model\BSONArray; | ||
use MongoDB\Model\BSONDocument; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use PHPUnit\Framework\Attributes\TestWith; | ||
|
||
|
@@ -907,14 +909,8 @@ public function testRaw(): void | |
$this->assertInstanceOf(EloquentCollection::class, $users); | ||
$this->assertInstanceOf(User::class, $users[0]); | ||
|
||
$user = User::raw(function (Collection $collection) { | ||
return $collection->findOne(['age' => 35]); | ||
}); | ||
|
||
$this->assertTrue(Model::isDocumentModel($user)); | ||
|
||
$count = User::raw(function (Collection $collection) { | ||
return $collection->count(); | ||
return $collection->estimatedDocumentCount(); | ||
}); | ||
$this->assertEquals(3, $count); | ||
|
||
|
@@ -924,6 +920,59 @@ public function testRaw(): void | |
$this->assertNotNull($result); | ||
} | ||
|
||
#[DataProvider('provideTypeMap')] | ||
public function testRawHyradeModel(array $typeMap): void | ||
GromNaN marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
User::insert([ | ||
['name' => 'John Doe', 'age' => 35, 'embed' => ['foo' => 'bar'], 'list' => [1, 2, 3]], | ||
['name' => 'Jane Doe', 'age' => 35, 'embed' => ['foo' => 'bar'], 'list' => [1, 2, 3]], | ||
['name' => 'Harry Hoe', 'age' => 15, 'embed' => ['foo' => 'bar'], 'list' => [1, 2, 3]], | ||
]); | ||
|
||
// Single document result | ||
$user = User::raw(fn (Collection $collection) => $collection->findOne( | ||
['age' => 35], | ||
[ | ||
'projection' => ['_id' => 1, 'name' => 1, 'age' => 1, 'now' => '$$NOW', 'embed' => 1, 'list' => 1], | ||
'typeMap' => $typeMap, | ||
], | ||
)); | ||
|
||
$this->assertInstanceOf(User::class, $user); | ||
$this->assertArrayNotHasKey('_id', $user->getAttributes()); | ||
$this->assertArrayHasKey('id', $user->getAttributes()); | ||
$this->assertNotEmpty($user->id); | ||
$this->assertInstanceOf(Carbon::class, $user->now); | ||
$this->assertEquals(['foo' => 'bar'], (array) $user->embed); | ||
$this->assertEquals([1, 2, 3], (array) $user->list); | ||
Comment on lines
+946
to
+947
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. We don't convert nested documents and array into array for single result. This is easily done for cursor and BSON documents with typemap. |
||
|
||
// Cursor result | ||
$result = User::raw(fn (Collection $collection) => $collection->aggregate([ | ||
['$set' => ['now' => '$$NOW']], | ||
['$limit' => 2], | ||
], ['typeMap' => $typeMap])); | ||
|
||
$this->assertInstanceOf(EloquentCollection::class, $result); | ||
$this->assertCount(2, $result); | ||
$user = $result->first(); | ||
$this->assertInstanceOf(User::class, $user); | ||
$this->assertArrayNotHasKey('_id', $user->getAttributes()); | ||
$this->assertArrayHasKey('id', $user->getAttributes()); | ||
$this->assertNotEmpty($user->id); | ||
$this->assertInstanceOf(Carbon::class, $user->now); | ||
$this->assertEquals(['foo' => 'bar'], $user->embed); | ||
$this->assertEquals([1, 2, 3], $user->list); | ||
} | ||
|
||
public static function provideTypeMap(): Generator | ||
{ | ||
yield 'default' => [[]]; | ||
yield 'array' => [['root' => 'array', 'document' => 'array', 'array' => 'array']]; | ||
yield 'object' => [['root' => 'object', 'document' => 'object', 'array' => 'array']]; | ||
yield 'Library BSON' => [['root' => BSONDocument::class, 'document' => BSONDocument::class, 'array' => BSONArray::class]]; | ||
yield 'Driver BSON' => [['root' => 'bson', 'document' => 'bson', 'array' => 'bson']]; | ||
GromNaN marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
public function testDotNotation(): void | ||
{ | ||
$user = User::create([ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1381,6 +1381,31 @@ function (Builder $elemMatchQuery): void { | |
->orWhereAny(['last_name', 'email'], 'not like', '%Doe%'), | ||
'orWhereAny', | ||
]; | ||
|
||
yield 'raw filter with _id and date' => [ | ||
[ | ||
'find' => [ | ||
[ | ||
'$and' => [ | ||
[ | ||
'$or' => [ | ||
['foo._id' => 1], | ||
['created_at' => ['$gte' => new UTCDateTime(new DateTimeImmutable('2018-09-30 00:00:00.000 +00:00'))]], | ||
], | ||
], | ||
['age' => 15], | ||
], | ||
], | ||
[], // options | ||
], | ||
], | ||
fn (Builder $builder) => $builder->where([ | ||
'$or' => [ | ||
Comment on lines
+1385
to
+1403
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. Added this test case to ensure |
||
['foo.id' => 1], | ||
['created_at' => ['$gte' => new DateTimeImmutable('2018-09-30 00:00:00 +00:00')]], | ||
], | ||
])->where('age', 15), | ||
]; | ||
} | ||
|
||
#[DataProvider('provideExceptions')] | ||
|
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.
Confirmed that PHP correctly calls the
has_property
handler when usingproperty_exists
: https://github.com/php/php-src/blob/1be989bbf0e46db8438384db184fca424392cfef/Zend/zend_builtin_functions.c#L1020..L1023