Sokil\Mongo\Collection::getDocumentDirectly PHP Method

getDocumentDirectly() public method

Get document by id directly omitting cache Method may return document as array if cursor configured through Cursor::asArray()
public getDocumentDirectly ( 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 getDocumentDirectly($id, $callable = null)
    {
        $cursor = $this->find();
        if (is_callable($callable)) {
            call_user_func($callable, $cursor);
        }
        return $cursor->byId($id)->skipDocumentPool()->findOne();
    }

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::getDocumentDirectly