Sokil\Mongo\Collection::createDocument PHP Method

createDocument() public method

Factory method to get not stored Document instance from array
public createDocument ( array $data = null ) : Document
$data array
return Document
    public function createDocument(array $data = null)
    {
        $className = $this->getDocumentClassName($data);
        /* @var $document \Sokil\Mongo\Document */
        $document = new $className($this, $data, array('stored' => false) + $this->definition->getOptions());
        // store document to identity map
        if ($this->isDocumentPoolEnabled()) {
            $collection = $this;
            $document->onAfterInsert(function (\Sokil\Mongo\Event $event) use($collection) {
                $collection->addDocumentToDocumentPool($event->getTarget());
            });
        }
        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::createDocument