/**
  * Lood document by its identifier.
  *
  * @param string $id
  * @return object|null
  */
 public function loadById($id)
 {
     $result = $this->_collection->findOne(array('_id' => $this->_class->getDatabaseIdentifierValue($id)));
     if ($result !== null) {
         $document = $this->_uow->getOrCreateDocument($this->_documentName, $result);
         $this->_uow->registerManaged($document, $this->_class->getPHPIdentifierValue($result['_id']), $result);
         return $document;
     }
     return null;
 }
Пример #2
0
 /**
  * Creates or fills a single document object from an query result.
  *
  * @param object $result The query result.
  * @param object $document The document object to fill, if any.
  * @param array $hints Hints for document creation.
  * @return object The filled and managed document object or NULL, if the query result is empty.
  */
 private function createDocument($result, $document = null, array $hints = array())
 {
     if ($result === null) {
         return null;
     }
     if ($document !== null) {
         $hints[Query::HINT_REFRESH] = true;
         $id = $this->class->getPHPIdentifierValue($result['_id']);
         $this->uow->registerManaged($document, $id, $result);
     }
     return $this->uow->getOrCreateDocument($this->class->name, $result, $hints, $document);
 }
 /**
  * Executes all queued document insertions and returns any generated post-insert
  * identifiers that were created as a result of the insertions.
  *
  * If no inserts are queued, invoking this method is a NOOP.
  *
  * @param array $options Array of options to be used with batchInsert()
  * @return array An array of any generated post-insert IDs. This will be an empty array
  *               if the document class does not use the IDENTITY generation strategy.
  */
 public function executeInserts(array $options = array())
 {
     if (!$this->queuedInserts) {
         return;
     }
     $postInsertIds = array();
     $inserts = array();
     foreach ($this->queuedInserts as $oid => $document) {
         $data = $this->pb->prepareInsertData($document);
         if (!$data) {
             continue;
         }
         // Set the initial version for each insert
         if ($this->class->isVersioned) {
             $versionMapping = $this->class->fieldMappings[$this->class->versionField];
             if ($versionMapping['type'] === 'int') {
                 $currentVersion = $this->class->reflFields[$this->class->versionField]->getValue($document);
                 $data[$versionMapping['name']] = $currentVersion;
                 $this->class->reflFields[$this->class->versionField]->setValue($document, $currentVersion);
             } elseif ($versionMapping['type'] === 'date') {
                 $nextVersion = new \DateTime();
                 $data[$versionMapping['name']] = new \MongoDate($nextVersion->getTimestamp());
                 $this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersion);
             }
         }
         $inserts[$oid] = $data;
     }
     if (empty($inserts)) {
         return;
     }
     $this->collection->batchInsert($inserts, $options);
     foreach ($inserts as $oid => $data) {
         $document = $this->queuedInserts[$oid];
         $postInsertIds[] = array($this->class->getPHPIdentifierValue($data['_id']), $document);
     }
     $this->queuedInserts = array();
     return $postInsertIds;
 }
Пример #4
0
 /**
  * @param ClassMetadata $class
  * @param object $document
  * @throws \InvalidArgumentException If there is something wrong with document's identifier.
  */
 private function persistNew(ClassMetadata $class, $document)
 {
     $oid = spl_object_hash($document);
     if (!empty($class->lifecycleCallbacks[Events::prePersist])) {
         $class->invokeLifecycleCallbacks(Events::prePersist, $document);
     }
     if ($this->evm->hasListeners(Events::prePersist)) {
         $this->evm->dispatchEvent(Events::prePersist, new LifecycleEventArgs($document, $this->dm));
     }
     $upsert = false;
     if ($class->identifier) {
         $idValue = $class->getIdentifierValue($document);
         $upsert = !$class->isEmbeddedDocument && $idValue !== null;
         if ($class->generatorType === ClassMetadata::GENERATOR_TYPE_NONE && $idValue === null) {
             throw new \InvalidArgumentException(sprintf("%s uses NONE identifier generation strategy but no identifier was provided when persisting.", get_class($document)));
         }
         // \MongoId::isValid($idValue) was introduced in 1.5.0 so it's no good
         if ($class->generatorType === ClassMetadata::GENERATOR_TYPE_AUTO && $idValue !== null && !preg_match('/^[0-9a-f]{24}$/', $idValue)) {
             throw new \InvalidArgumentException(sprintf("%s uses AUTO identifier generation strategy but provided identifier is not valid MongoId.", get_class($document)));
         }
         if ($class->generatorType !== ClassMetadata::GENERATOR_TYPE_NONE && $idValue === null) {
             $idValue = $class->idGenerator->generate($this->dm, $document);
             $idValue = $class->getPHPIdentifierValue($class->getDatabaseIdentifierValue($idValue));
             $class->setIdentifierValue($document, $idValue);
         }
         $this->documentIdentifiers[$oid] = $idValue;
     } else {
         // this is for embedded documents without identifiers
         $this->documentIdentifiers[$oid] = $oid;
     }
     $this->documentStates[$oid] = self::STATE_MANAGED;
     if ($upsert) {
         $this->scheduleForUpsert($class, $document);
     } else {
         $this->scheduleForInsert($class, $document);
     }
 }
 /**
  * Returns the reference representation to be stored in mongodb or null if not applicable.
  *
  * @param ClassMetadata $class
  * @param Document $doc
  * @return array|null
  */
 private function _prepareDocReference(ClassMetadata $class, $doc)
 {
     if (!is_object($doc)) {
         return $doc;
     }
     $id = $this->_uow->getDocumentIdentifier($doc);
     if (null !== $id) {
         $id = $class->getPHPIdentifierValue($id);
     }
     $ref = array($this->_cmd . 'ref' => $class->getCollection(), $this->_cmd . 'id' => $id, $this->_cmd . 'db' => $class->getDB());
     return $ref;
 }