Преглед изворни кода

Merge pull request #149 from alcaeus/get-dbref-with-db-param

Use database stored in DBRef object
Andreas пре 9 година
родитељ
комит
079e4103ff
3 измењених фајлова са 36 додато и 1 уклоњено
  1. 14 0
      CHANGELOG-1.0.md
  2. 2 1
      lib/Mongo/MongoDB.php
  3. 20 0
      tests/Alcaeus/MongoDbAdapter/Mongo/MongoDBRefTest.php

+ 14 - 0
CHANGELOG-1.0.md

@@ -3,6 +3,20 @@ CHANGELOG
 
 This changelog references the relevant changes done in minor version updates.
 
+1.0.8 (2017-xx-xx)
+------------------
+
+All issues and pull requests under this release may be found under the
+[1.0.8](https://github.com/alcaeus/mongo-php-adapter/issues?q=milestone%3A1.0.8)
+milestone.
+
+ * [#149](https://github.com/alcaeus/mongo-php-adapter/pull/149) fixes calls to
+ `MongoDB::getDBRef` with references containing a `$db` field with a different
+ value than the current database.
+ * [#145](https://github.com/alcaeus/mongo-php-adapter/pull/145) fixes query
+ projections in the legacy syntax. While never documented, this was happily
+ accepted by the legacy driver.
+
 1.0.7 (2016-12-18)
 ------------------
 

+ 2 - 1
lib/Mongo/MongoDB.php

@@ -376,7 +376,8 @@ class MongoDB
      */
     public function getDBRef(array $ref)
     {
-        return MongoDBRef::get($this, $ref);
+        $db = (isset($ref['$db']) && $ref['$db'] !== $this->name) ? $this->connection->selectDB($ref['$db']) : $this;
+        return MongoDBRef::get($db, $ref);
     }
 
     /**

+ 20 - 0
tests/Alcaeus/MongoDbAdapter/Mongo/MongoDBRefTest.php

@@ -116,4 +116,24 @@ class MongoDBRefTest extends TestCase
 
         $this->assertNull(\MongoDBRef::get($db, []));
     }
+
+    public function testGetWithDifferentDatabase()
+    {
+        $database = $this->getDatabase();
+        $collection = $this->getCollection();
+
+        $document = ['foo' => 'bar'];
+
+        $collection->insert($document);
+
+        $ref = [
+            '$ref' => $collection->getName(),
+            '$id' => $document['_id'],
+            '$db' => (string) $database,
+        ];
+
+        $referencedDocument = $this->getClient()->selectDB('foo')->getDBRef($ref);
+
+        $this->assertEquals($document, $referencedDocument);
+    }
 }