Sokil\Mongo\Database::getDocumentByReference PHP Method

getDocumentByReference() public method

Get Document instance by it's reference
public getDocumentByReference ( array $ref, boolean $useDocumentPool = true ) : Document | null
$ref array reference to document
$useDocumentPool boolean try to get document from pool or fetch document from database
return Document | null
    public function getDocumentByReference(array $ref, $useDocumentPool = true)
    {
        $documentArray = $this->getMongoDB()->getDBRef($ref);
        if (null === $documentArray) {
            return null;
        }
        return $this->getCollection($ref['$ref'])->hydrate($documentArray, $useDocumentPool);
    }

Usage Example

Example #1
0
 public function testGetDocumentByReference()
 {
     $collection = $this->database->getCollection('phpmongo_test_collection');
     $collection->delete();
     // create document
     $document = $collection->createDocument(array('param' => 'value'))->save();
     // invalid col and db
     $foundDocument = $this->database->getDocumentByReference(array('$ref' => 'some_collection', '$db' => 'some_db', '$id' => $document->getId()), false);
     $this->assertNull($foundDocument);
     /// invalid db
     $foundDocument = $this->database->getDocumentByReference(array('$ref' => $collection->getName(), '$db' => 'some_db', '$id' => $document->getId()), false);
     $this->assertNull($foundDocument);
     // all valid
     $foundDocument = $this->database->getDocumentByReference(array('$ref' => $collection->getName(), '$db' => $collection->getDatabase()->getName(), '$id' => $document->getId()), false);
     $this->assertSame((string) $document->getId(), (string) $foundDocument->getId());
     $collection->delete();
 }