/** * 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(); $upserts = array(); foreach ($this->queuedInserts as $oid => $document) { $upsert = $this->uow->isScheduledForUpsert($document); if ($upsert) { $data = $this->pb->prepareUpsertData($document); } else { $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') { $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); } } if ($upsert) { $upserts[$oid] = $data; } else { $inserts[$oid] = $data; } } if ($inserts) { $this->collection->batchInsert($inserts, $options); foreach ($inserts as $oid => $data) { $document = $this->queuedInserts[$oid]; $postInsertIds[] = array($this->class->getPHPIdentifierValue($data['_id']), $document); } } if ($upserts) { $upsertOptions = $options; $upsertOptions['upsert'] = true; foreach ($upserts as $oid => $data) { $criteria = array('_id' => $data[$this->cmd . 'set']['_id']); unset($data[$this->cmd . 'set']['_id']); // stupid php if (empty($data[$this->cmd . 'set'])) { $data[$this->cmd . 'set'] = new \stdClass(); } $this->collection->update($criteria, $data, $upsertOptions); } } $this->queuedInserts = array(); return $postInsertIds; }
/** * 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; }