Procházet zdrojové kódy

Merge pull request #77 from alcaeus/find-and-modify-update-option

Support update option in findAndModify calls
Andreas před 9 roky
rodič
revize
59a3bc6332

+ 7 - 1
lib/Mongo/MongoCollection.php

@@ -500,7 +500,13 @@ class MongoCollection
                 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;

+ 26 - 0
tests/Alcaeus/MongoDbAdapter/Mongo/MongoCollectionTest.php

@@ -1106,6 +1106,32 @@ class MongoCollectionTest extends TestCase
         $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';