Explorar o código

Use database stored in DBRef object

Andreas Braun %!s(int64=9) %!d(string=hai) anos
pai
achega
dd5d3dc79b

+ 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);
+    }
 }