Prechádzať zdrojové kódy

Ensure array is given to insertMany()

Andreas Braun 10 rokov pred
rodič
commit
144f4a77c6

+ 1 - 1
lib/Mongo/MongoCollection.php

@@ -285,7 +285,7 @@ class MongoCollection
     public function batchInsert(array $a, array $options = [])
     {
         $result = $this->collection->insertMany(
-            TypeConverter::fromLegacy($a),
+            TypeConverter::fromLegacy(array_values($a)),
             $this->convertWriteConcernOptions($options)
         );
 

+ 18 - 0
tests/Alcaeus/MongoDbAdapter/MongoCollectionTest.php

@@ -61,6 +61,24 @@ class MongoCollectionTest extends TestCase
         $this->assertSame($expected, $this->getCollection()->batchInsert($documents));
     }
 
+    public function testInsertManyWithNonNumericKeys()
+    {
+        $expected = [
+            'connectionId' => 0,
+            'n' => 0,
+            'syncMillis' => 0,
+            'writtenTo' => null,
+            'err' => null,
+            'errmsg' => null
+        ];
+
+        $documents = [
+            'a' => ['foo' => 'bar'],
+            'b' => ['bar' => 'foo']
+        ];
+        $this->assertSame($expected, $this->getCollection()->batchInsert($documents));
+    }
+
     public function testUpdateOne()
     {
         $this->getCollection()->insert(['foo' => 'bar']);