/** * Executes all queued document upserts. * * Queued documents with an ID are upserted individually. * * If no upserts are queued, invoking this method is a NOOP. * * @param array $options Options for batchInsert() and update() driver methods */ public function executeUpserts(array $options = array()) { if (!$this->queuedUpserts) { return; } foreach ($this->queuedUpserts as $oid => $document) { $data = $this->pb->prepareUpsertData($document); // Set the initial version for each upsert 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['$set'][$versionMapping['name']] = $nextVersion; } try { $this->executeUpsert($data, $options); $this->handleCollections($document, $options); unset($this->queuedUpserts[$oid]); } catch (\MongoException $e) { unset($this->queuedUpserts[$oid]); throw $e; } } }