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-239 Convert _id and UTCDateTime in results of Model::raw() before hydratation #3152

Merged
merged 5 commits into from
Sep 17, 2024

Conversation

GromNaN
Copy link
Member

@GromNaN GromNaN commented Sep 13, 2024

Fix PHPORM-239
Fix #3151

Checklist

  • Add tests and ensure they pass
  • Add an entry to the CHANGELOG.md file

@GromNaN
Copy link
Member Author

GromNaN commented Sep 13, 2024

I don't see any way to change this behavior without breaking backward compatibility.

@GromNaN GromNaN marked this pull request as ready for review September 17, 2024 10:42
@GromNaN GromNaN requested a review from a team as a code owner September 17, 2024 10:42
@GromNaN GromNaN force-pushed the PHPORM-239 branch 2 times, most recently from 10c98ee to 7f0b77e Compare September 17, 2024 10:44

return $this->model->newFromBuilder((array) $results);
// Convert MongoDB Document to a single object.
if (is_object($results) && (property_exists($results, '_id') || property_exists($results, 'id'))) {
Copy link
Member

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 using property_exists: https://github.com/php/php-src/blob/1be989bbf0e46db8438384db184fca424392cfef/Zend/zend_builtin_functions.c#L1020..L1023

tests/ModelTest.php Show resolved Hide resolved
tests/ModelTest.php Show resolved Hide resolved
@GromNaN GromNaN changed the title PHPORM-239 Apply result transformation to raw() queries PHPORM-239 Convert _id and UTCDateTime in results of Model::raw() before hydratation Sep 17, 2024
@alcaeus
Copy link
Member

alcaeus commented Sep 17, 2024

LGTM, but tests need a little more work.

Comment on lines +946 to +947
$this->assertEquals(['foo' => 'bar'], (array) $user->embed);
$this->assertEquals([1, 2, 3], (array) $user->list);
Copy link
Member Author

Choose a reason for hiding this comment

The 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.

Comment on lines +1385 to +1403
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' => [
Copy link
Member Author

Choose a reason for hiding this comment

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

Added this test case to ensure DB::where($filter)->get() is identical to $collection->find($filter) (for the doc team).

@GromNaN GromNaN merged commit 38dc1e3 into mongodb:5.x Sep 17, 2024
27 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

_id returned by raw()
2 participants