Просмотр исходного кода

Merge pull request #52 from alcaeus/fix-update-incompatibility

Fix calls to update() without a $ operator
Andreas 10 лет назад
Родитель
Сommit
662a8c0029

+ 8 - 1
lib/Mongo/MongoCollection.php

@@ -366,9 +366,16 @@ class MongoCollection
     public function update(array $criteria , array $newobj, array $options = [])
     {
         $multiple = isset($options['multiple']) ? $options['multiple'] : false;
-        $method = $multiple ? 'updateMany' : 'updateOne';
+        $isReplace = ! \MongoDB\is_first_key_operator($newobj);
+
+        if ($isReplace && $multiple) {
+            throw new \MongoWriteConcernException('multi update only works with $ operators', 9);
+        }
         unset($options['multiple']);
 
+        $method = $isReplace ? 'replace' : 'update';
+        $method .= $multiple ? 'Many' : 'One';
+
         try {
             /** @var \MongoDB\UpdateResult $result */
             $result = $this->collection->$method(

+ 32 - 1
tests/Alcaeus/MongoDbAdapter/MongoCollectionTest.php

@@ -176,7 +176,7 @@ class MongoCollectionTest extends TestCase
 
     public function testBatchInsertException()
     {
-        $this->setExpectedException('MongoDuplicateKeyException', 'E11000 duplicate key error index: mongo-php-adapter.test.$_id_');
+        $this->setExpectedExceptionRegExp('MongoDuplicateKeyException', '/E11000 duplicate key error .* mongo-php-adapter.test.*_id_/');
 
         $id = new \MongoId();
         $documents = [['_id' => $id, 'foo' => 'bar'], ['_id' => $id, 'foo' => 'bleh']];
@@ -222,6 +222,37 @@ class MongoCollectionTest extends TestCase
         $this->assertSame(1, $this->getCheckDatabase()->selectCollection('test')->count(['foo' => 'foo']));
     }
 
+    public function testUpdateReplaceOne()
+    {
+        $document = ['foo' => 'bar', 'bar' => 'foo'];
+        $this->getCollection()->insert($document);
+
+        // Unset ID to re-insert
+        unset($document['_id']);
+        $this->getCollection()->insert($document);
+
+        $expected = [
+            'ok' => 1.0,
+            'nModified' => 1,
+            'n' => 1,
+            'err' => null,
+            'errmsg' => null,
+            'updatedExisting' => true,
+        ];
+
+        $result = $this->getCollection()->update(['foo' => 'bar'], ['foo' => 'foo']);
+        $this->assertSame($expected, $result);
+
+        $this->assertSame(1, $this->getCheckDatabase()->selectCollection('test')->count(['foo' => 'foo']));
+        $this->assertSame(1, $this->getCheckDatabase()->selectCollection('test')->count(['bar' => 'foo']));
+    }
+
+    public function testUpdateReplaceMultiple()
+    {
+        $this->setExpectedExceptionRegExp('MongoWriteConcernException', '/multi update only works with \$ operators/', 9);
+        $this->getCollection()->update(['foo' => 'bar'], ['foo' => 'foo'], ['multiple' => true]);
+    }
+
     public function testUpdateDuplicate()
     {
         $collection = $this->getCollection();