Skip to content

Commit

Permalink
Merge pull request #77 from alcaeus/find-and-modify-update-option
Browse files Browse the repository at this point in the history
Support update option in findAndModify calls
  • Loading branch information
alcaeus committed Mar 10, 2016
2 parents 14dddf0 + cf4e316 commit 59a3bc6
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/Mongo/MongoCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,13 @@ public function findAndModify(array $query, array $update = null, array $fields
unset($options['remove']);
$document = $this->collection->findOneAndDelete($query, $options);
} else {
$update = is_array($update) ? TypeConverter::fromLegacy($update) : [];
$update = is_array($update) ? $update : [];
if (isset($options['update']) && is_array($options['update'])) {
$update = array_merge($update, $options['update']);
unset($options['update']);
}

$update = TypeConverter::fromLegacy($update);

if (isset($options['new'])) {
$options['returnDocument'] = \MongoDB\Operation\FindOneAndUpdate::RETURN_DOCUMENT_AFTER;
Expand Down
26 changes: 26 additions & 0 deletions tests/Alcaeus/MongoDbAdapter/Mongo/MongoCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1106,6 +1106,32 @@ public function testFindAndModifyUpdate()
$this->assertAttributeSame('foo', 'foo', $object);
}

public function testFindAndModifyUpdateWithUpdateOptions()
{
$id = '54203e08d51d4a1f868b456e';
$collection = $this->getCollection();

$document = ['_id' => new \MongoId($id), 'foo' => 'bar'];
$collection->insert($document);
$document = $collection->findAndModify(
['_id' => new \MongoId($id)],
[],
[],
[
'update' => ['bar' => 'foo']
]
);
$this->assertSame('bar', $document['foo']);

$newCollection = $this->getCheckDatabase()->selectCollection('test');
$this->assertSame(1, $newCollection->count());
$object = $newCollection->findOne();

$this->assertNotNull($object);
$this->assertAttributeSame('foo', 'bar', $object);
$this->assertObjectNotHasAttribute('foo', $object);
}

public function testFindAndModifyUpdateReplace()
{
$id = '54203e08d51d4a1f868b456e';
Expand Down

0 comments on commit 59a3bc6

Please sign in to comment.