Ejemplo n.º 1
0
 /**
  * Executes all queued document insertions.
  *
  * Queued documents without an ID will inserted in a batch and queued
  * documents with an ID will be upserted individually.
  *
  * If no inserts are queued, invoking this method is a NOOP.
  *
  * @param array $options Options for batchInsert() and update() driver methods
  */
 public function executeInserts(array $options = array())
 {
     if (!$this->queuedInserts) {
         return;
     }
     $inserts = array();
     foreach ($this->queuedInserts as $oid => $document) {
         $data = $this->pb->prepareInsertData($document);
         // Set the initial version for each insert
         if ($this->class->isVersioned) {
             $versionMapping = $this->class->fieldMappings[$this->class->versionField];
             if ($versionMapping['type'] === 'int') {
                 $nextVersion = max(1, (int) $this->class->reflFields[$this->class->versionField]->getValue($document));
                 $this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersion);
             } elseif ($versionMapping['type'] === 'date') {
                 $nextVersionDateTime = new \DateTime();
                 $nextVersion = new \MongoDate($nextVersionDateTime->getTimestamp());
                 $this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersionDateTime);
             }
             $data[$versionMapping['name']] = $nextVersion;
         }
         $inserts[$oid] = $data;
     }
     if ($inserts) {
         try {
             $this->collection->batchInsert($inserts, $options);
         } catch (\MongoException $e) {
             $this->queuedInserts = array();
             throw $e;
         }
     }
     /* All collections except for ones using addToSet have already been
      * saved. We have left these to be handled separately to avoid checking
      * collection for uniqueness on PHP side.
      */
     foreach ($this->queuedInserts as $document) {
         $this->handleCollections($document, $options);
     }
     $this->queuedInserts = array();
 }