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-139 Improve Model::createOrFirst() tests and error checking #2759

Merged
merged 4 commits into from
Mar 13, 2024

Conversation

GromNaN
Copy link
Member

@GromNaN GromNaN commented Mar 11, 2024

Applies @jmikola's review on #2742
Fix PHPORM-139

@GromNaN GromNaN added this to the 4.2 milestone Mar 11, 2024
@GromNaN GromNaN requested a review from jmikola March 11, 2024 18:16
@GromNaN GromNaN requested a review from a team as a code owner March 11, 2024 18:16
Copy link
Member

@jmikola jmikola left a comment

Choose a reason for hiding this comment

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

Two outstanding points re: the findOneAndUpdate() call:

src/Eloquent/Builder.php Show resolved Hide resolved
['$setOnInsert' => (object) $values],
[
'upsert' => true,
'returnDocument' => FindOneAndUpdate::RETURN_DOCUMENT_AFTER,
Copy link
Member Author

Choose a reason for hiding this comment

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

new option was internal. #2742 (comment)

@@ -207,8 +214,12 @@ public function createOrFirst(array $attributes = [], array $values = []): Model
try {
$document = $collection->findOneAndUpdate(
$attributes,
['$setOnInsert' => $values],
['upsert' => true, 'new' => true, 'typeMap' => ['root' => 'array', 'document' => 'array']],
['$setOnInsert' => (object) $values],
Copy link
Member Author

Choose a reason for hiding this comment

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

I added the (object) cast, but since we need to support server version 4.4, I need to keep an non-empty object (using filter attributes). #2742 (comment)

Copy link
Member

Choose a reason for hiding this comment

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

The object cast certainly doesn't hurt and is arguably correct in the event all field names were numeric (if that's even possible here).

I do think it's worth adding a comment to explain that you're intentionally keeping extra fields in $values (instead of filtering down to keys in the original param) to ensure compatibility with pre-5.0 server versions.

@GromNaN GromNaN requested review from jmikola and alcaeus March 12, 2024 08:34
jmikola
jmikola previously approved these changes Mar 12, 2024
Copy link
Member

@jmikola jmikola left a comment

Choose a reason for hiding this comment

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

Suggested a comment above $setOnInsert line, but code changes LGTM.

jmikola
jmikola previously approved these changes Mar 13, 2024
src/Eloquent/Builder.php Outdated Show resolved Hide resolved
@GromNaN GromNaN requested a review from jmikola March 13, 2024 14:13
@GromNaN GromNaN enabled auto-merge (squash) March 13, 2024 14:54
@GromNaN GromNaN merged commit f5f86c8 into mongodb:4.2 Mar 13, 2024
23 checks passed
@GromNaN GromNaN deleted the PHPORM-139 branch March 13, 2024 22:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants