-
Notifications
You must be signed in to change notification settings - Fork 1.5k
PHPORM-139 Improve Model::createOrFirst()
tests and error checking
#2759
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
Changes from 3 commits
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 |
---|---|---|
|
@@ -6,11 +6,13 @@ | |
|
||
use Illuminate\Database\ConnectionInterface; | ||
use Illuminate\Database\Eloquent\Builder as EloquentBuilder; | ||
use InvalidArgumentException; | ||
use MongoDB\Driver\Cursor; | ||
use MongoDB\Laravel\Collection; | ||
use MongoDB\Laravel\Helpers\QueriesRelationships; | ||
use MongoDB\Laravel\Internal\FindAndModifyCommandSubscriber; | ||
use MongoDB\Model\BSONDocument; | ||
use MongoDB\Operation\FindOneAndUpdate; | ||
|
||
use function array_intersect_key; | ||
use function array_key_exists; | ||
|
@@ -195,7 +197,12 @@ public function raw($value = null) | |
*/ | ||
public function createOrFirst(array $attributes = [], array $values = []): Model | ||
{ | ||
if ($attributes === []) { | ||
throw new InvalidArgumentException('You must provide attributes to check for duplicates'); | ||
} | ||
|
||
// Apply casting and default values to the attributes | ||
// In case of duplicate key between the attributes and the values, the values have priority | ||
$instance = $this->newModelInstance($values + $attributes); | ||
$values = $instance->getAttributes(); | ||
$attributes = array_intersect_key($attributes, $values); | ||
|
@@ -207,8 +214,14 @@ public function createOrFirst(array $attributes = [], array $values = []): Model | |
try { | ||
$document = $collection->findOneAndUpdate( | ||
$attributes, | ||
['$setOnInsert' => $values], | ||
['upsert' => true, 'new' => true, 'typeMap' => ['root' => 'array', 'document' => 'array']], | ||
// Before MongoDB 5.0, the $setOnInsert does support empty document, | ||
// so the filter value are added to avoid an error. | ||
GromNaN marked this conversation as resolved.
Show resolved
Hide resolved
|
||
['$setOnInsert' => (object) $values], | ||
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. I added the 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. 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 |
||
[ | ||
'upsert' => true, | ||
'returnDocument' => FindOneAndUpdate::RETURN_DOCUMENT_AFTER, | ||
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.
|
||
'typeMap' => ['root' => 'array', 'document' => 'array'], | ||
], | ||
); | ||
} finally { | ||
$collection->getManager()->removeSubscriber($listener); | ||
|
Uh oh!
There was an error while loading. Please reload this page.