Sokil\Mongo\Collection::getDocument PHP Method

getDocument() public method

Method may return document as array if cursor configured through Cursor::asArray()
public getDocument ( string | MongoId $id, callable $callable = null ) : Document | array | null
$id string | MongoId
$callable callable cursor callable used to configure cursor
return Document | array | null
    public function getDocument($id, $callable = null)
    {
        if (!$this->isDocumentPoolEnabled()) {
            return $this->getDocumentDirectly($id, $callable);
        }
        if (!$callable && $this->isDocumentInDocumentPool($id)) {
            return $this->getDocumentFromDocumentPool($id);
        }
        $document = $this->getDocumentDirectly($id, $callable);
        // if callable configure cursor to return document as array,
        // than it can't be stored to document pool
        if ($document instanceof Document) {
            $this->addDocumentToDocumentPool($document);
        }
        return $document;
    }

Usage Example

Example #1
0
 /**
  * Abstract test
  *
  * @param mixed $value1 first value to push
  * @param mixed $value2 second value to push
  * @param array $expectedList expected list, stored in db
  * @param int $fieldName name of field, where values pushed: one of self::FIELD_NAME_*
  * @param bool $isDocumentSaved is document already stored to db before push
  */
 private function doPushTest($value1, $value2, $expectedList, $fieldName, $isDocumentSaved)
 {
     // create document
     $doc = $this->collection->createDocument($this->initialDocument);
     // test push to saved or new document
     if ($isDocumentSaved) {
         $doc->save();
     }
     // prepare expected value
     switch ($fieldName) {
         case self::FIELD_NAME_SCALAR:
             $expectedList = array_merge(array($this->initialDocument[$fieldName]), $expectedList);
             break;
         case self::FIELD_NAME_LIST:
             $expectedList = array_merge($this->initialDocument[$fieldName], $expectedList);
             break;
     }
     // push single to empty
     $doc->push($fieldName, $value1)->push($fieldName, $value2);
     // test document in identity map after push before save
     $this->assertEquals($expectedList, $doc->get($fieldName));
     $doc->save();
     // test document in identity map after push after save
     $this->assertEquals($expectedList, $this->collection->getDocument($doc->getId())->get($fieldName));
     // test document in db
     $this->assertEquals($expectedList, $this->collection->getDocumentDirectly($doc->getId())->get($fieldName));
 }
All Usage Examples Of Sokil\Mongo\Collection::getDocument