-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Conversation
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.
Two outstanding points re: the findOneAndUpdate()
call:
- Consider an explicit
(object)
cast when executing the query. Re: PHPORM-139 ImplementModel::createOrFirst()
usingfindOneAndUpdate
operation #2742 (comment) - Use
returnDocument
instead ofnew
. Re: PHPORM-139 ImplementModel::createOrFirst()
usingfindOneAndUpdate
operation #2742 (comment)
['$setOnInsert' => (object) $values], | ||
[ | ||
'upsert' => true, | ||
'returnDocument' => FindOneAndUpdate::RETURN_DOCUMENT_AFTER, |
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.
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], |
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.
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)
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.
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.
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.
Suggested a comment above $setOnInsert
line, but code changes LGTM.
Co-authored-by: Jeremy Mikola <[email protected]>
Applies @jmikola's review on #2742
Fix PHPORM-139