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