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-229 Make Query\Builder return objects instead of array to match Laravel behavior #3107

Merged
merged 1 commit into from
Aug 27, 2024

Conversation

GromNaN
Copy link
Member

@GromNaN GromNaN commented Aug 21, 2024

Fix PHPORM-229

Laravel Query Builder always returns objects (source).

This is a major breaking change that requires changing all array access into property access. Returning a MongoDB\Model\BSONDocument and MongoDB\Model\BSONArray is not possible as the data is casted with native PHP cast feature in Laravel code: (object) $result and (array) $result.

Checklist

  • Add tests and ensure they pass
  • Add an entry to the CHANGELOG.md file
  • Update documentation for new features

@GromNaN GromNaN added this to the 5.0 milestone Aug 21, 2024
@GromNaN GromNaN requested review from a team as code owners August 21, 2024 13:37
@github-actions github-actions bot added the docs label Aug 21, 2024
@GromNaN GromNaN changed the title PHPORM-229 Make Query\Builder return objects instead of array to matc… PHPORM-229 Make Query\Builder return objects instead of array to match Laravel's behavior Aug 21, 2024
@@ -451,8 +455,7 @@ public function toMql(): array
$options['projection'] = $projection;
}

// Fix for legacy support, converts the results to arrays instead of objects.
$options['typeMap'] = ['root' => 'array', 'document' => 'array'];
$options['typeMap'] = ['root' => 'object', 'document' => 'array'];
Copy link
Member Author

Choose a reason for hiding this comment

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

Shall we update the type of document to object also, to get the same type for root and embedded documents?

Copy link
Member

Choose a reason for hiding this comment

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

If the root document is returned as an object, it would make sense to return all embedded documents as objects as well.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, butLaravel have sort of embedded document with "JSON" fields. They are returned as array...

Copy link
Member

Choose a reason for hiding this comment

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

Ah, I see. In that case, let's leave it as-is for consistency with Laravel, even though it seems strange in general.

@GromNaN GromNaN changed the title PHPORM-229 Make Query\Builder return objects instead of array to match Laravel's behavior PHPORM-229 Make Query\Builder return objects instead of array to match Laravel behavior Aug 22, 2024
@masterbater
Copy link
Contributor

Is it possible that all v5 pr be merge this week? I just have an expectation that v5 will be release this week. But if not, no pressure just want those big changes to be merge so we can evaluate if we need to add rdbms or just this package can do fine itself.

@alcaeus
Copy link
Member

alcaeus commented Aug 26, 2024

data is casted with native PHP cast feature in Laravel code: (object) $result and (array) $result.

Note that we could leverage the cast_object object handler in extension classes to facilitate converting a PackedArray into an array.

@GromNaN
Copy link
Member Author

GromNaN commented Aug 26, 2024

data is casted with native PHP cast feature in Laravel code: (object) $result and (array) $result.

Note that we could leverage the cast_object object handler in extension classes to facilitate converting a PackedArray into an array.

Tracking in PHPC-2426

@GromNaN GromNaN requested a review from alcaeus August 26, 2024 10:05
@GromNaN GromNaN force-pushed the PHPORM-229 branch 3 times, most recently from 83a7f8e to fa2c542 Compare August 26, 2024 10:35
@GromNaN GromNaN enabled auto-merge (squash) August 27, 2024 21:57
@GromNaN GromNaN merged commit ebda1fa into mongodb:5.0 Aug 27, 2024
26 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.

4 participants