Beispiel #1
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);
 }
Beispiel #2
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);
     }
 }