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

Merge pull request #236 from alcaeus/allow-zero-key

Allow "0" to be used as a key
Andreas 7 лет назад
Родитель
Сommit
ae806ac488

+ 1 - 1
lib/Mongo/MongoCollection.php

@@ -983,7 +983,7 @@ class MongoCollection
     private function checkKeys(array $array)
     {
         foreach ($array as $key => $value) {
-            if (empty($key) && $key !== 0) {
+            if (empty($key) && $key !== 0 && $key !== '0') {
                 throw new \MongoException('zero-length keys are not allowed, did you use $ with double quotes?');
             }
 

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

@@ -130,6 +130,20 @@ class MongoCollectionTest extends TestCase
         $this->assertSame(1, $this->getCollection()->count(['foo']));
     }
 
+    public function testInsertWithAlphaNumbericKey()
+    {
+        /**
+         * Force the array to store the key as a string "0".
+         * Initialising like ['0' => 'foo'] casts the string to an int.
+         */
+        $document = new \stdClass();
+        $document->{'0'} = 'foo';
+        $document = (array) $document;
+
+        $this->getCollection()->insert($document);
+        $this->assertSame(1, $this->getCollection()->count(['0' => 'foo']));
+    }
+
     public function testInsertDuplicate()
     {
         $collection = $this->getCollection();