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
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));
 }
Example #2
0
 /**
  * @dataProvider persistanceInstanceProvider
  */
 public function testPersistUpdate(Persistence $persistence)
 {
     // store document
     $documentId = $this->collection->createDocument(array('param' => 'value'))->save()->getId();
     // get document directly and change
     $document = $this->collection->getDocumentDirectly($documentId);
     $document->param = 'new';
     // add document
     $persistence->persist($document)->flush();
     // get document directly and check
     $this->assertEquals('new', $this->collection->find()->findOne()->param);
 }
Example #3
0
 public function testCancelOperation_BeforeUpdate()
 {
     $document = $this->collection->delete()->createDocument(array('field' => 'value'))->save()->onBeforeUpdate(function (\Sokil\Mongo\Event $event, $eventName, $dispatcher) {
         $event->cancel();
     })->set('field', 'updatedValue')->save();
     $this->assertEquals('value', $this->collection->getDocumentDirectly($document->getId())->get('field'));
 }
Example #4
0
 public function testUpdate_ExpressionIsExpressionObject()
 {
     // create documents
     $d1 = $this->collection->createDocument(array('p' => 1))->save();
     $d2 = $this->collection->createDocument(array('p' => 2))->save();
     $this->collection->update($this->collection->expression()->where('p', 1), $this->collection->operator()->set('k', 'v'), array('multiple' => true));
     // test 1
     $data1 = $this->collection->getDocumentDirectly($d1->getId())->toArray();
     unset($data1['_id']);
     $this->assertEquals(array('p' => 1, 'k' => 'v'), $data1);
     // test 2
     $data2 = $this->collection->getDocumentDirectly($d2->getId())->toArray();
     unset($data2['_id']);
     $this->assertEquals(array('p' => 2), $data2);
 }
Example #5
0
 public function testMergeOnUpdate()
 {
     // save document
     $document = $this->collection->createDocument(array('p' => 'pv'))->save();
     // update document
     $document->set('f1', 'fv1')->merge(array('a1' => 'av1', 'a2' => 'av2'));
     $documentData = $document->toArray();
     unset($documentData['_id']);
     $this->assertEquals(array('p' => 'pv', 'f1' => 'fv1', 'a1' => 'av1', 'a2' => 'av2'), $documentData);
     $document->save();
     // test
     $foundDocumentData = $this->collection->getDocumentDirectly($document->getId())->toArray();
     unset($foundDocumentData['_id']);
     $this->assertEquals(array('p' => 'pv', 'f1' => 'fv1', 'a1' => 'av1', 'a2' => 'av2'), $foundDocumentData);
 }