Example #1
0
 /**
  * @param object[] $models
  * @param array  $options
  *
  * @return \MongoDB\InsertOneResult
  */
 public function insertMany(array $models, array $options = [])
 {
     $data = [];
     foreach ($models as $key => $model) {
         $data[$key] = get_object_values($model);
     }
     $result = $this->collection->insertMany($data, $options);
     if (false == $result->isAcknowledged()) {
         throw new \LogicException('Operation is not acknowledged');
     }
     foreach ($result->getInsertedIds() as $key => $modelId) {
         $this->hydrator->hydrate($data[$key], $models[$key]);
         set_object_id($models[$key], $modelId);
     }
     return $result;
 }
 /**
  * Inserts multiple documents into this collection
  *
  * @link http://www.php.net/manual/en/mongocollection.batchinsert.php
  * @param array $a An array of arrays.
  * @param array $options Options for the inserts.
  * @throws MongoCursorException
  * @return mixed If "safe" is set, returns an associative array with the status of the inserts ("ok") and any error that may have occured ("err"). Otherwise, returns TRUE if the batch insert was successfully sent, FALSE otherwise.
  */
 public function batchInsert(array &$a, array $options = [])
 {
     if (empty($a)) {
         throw new \MongoException('No write ops were included in the batch');
     }
     $continueOnError = isset($options['continueOnError']) && $options['continueOnError'];
     foreach ($a as $key => $item) {
         try {
             if (!$this->ensureDocumentHasMongoId($a[$key])) {
                 if ($continueOnError) {
                     unset($a[$key]);
                 } else {
                     trigger_error(sprintf('%s expects parameter %d to be an array or object, %s given', __METHOD__, 1, gettype($a)), E_USER_WARNING);
                     return;
                 }
             }
         } catch (MongoException $e) {
             if (!$continueOnError) {
                 throw $e;
             }
         }
     }
     try {
         $result = $this->collection->insertMany(TypeConverter::fromLegacy(array_values($a)), $this->convertWriteConcernOptions($options));
     } catch (\MongoDB\Driver\Exception\Exception $e) {
         ExceptionConverter::toLegacy($e);
     }
     if (!$result->isAcknowledged()) {
         return true;
     }
     return ['connectionId' => 0, 'n' => 0, 'syncMillis' => 0, 'writtenTo' => null, 'err' => null, 'errmsg' => null];
 }
 /**
  * Inserts multiple documents into this collection
  *
  * @link http://www.php.net/manual/en/mongocollection.batchinsert.php
  * @param array $a An array of arrays.
  * @param array $options Options for the inserts.
  * @throws MongoCursorException
  * @return mixed If "safe" is set, returns an associative array with the status of the inserts ("ok") and any error that may have occured ("err"). Otherwise, returns TRUE if the batch insert was successfully sent, FALSE otherwise.
  */
 public function batchInsert(array &$a, array $options = [])
 {
     foreach ($a as $key => $item) {
         $this->ensureDocumentHasMongoId($a[$key]);
     }
     $result = $this->collection->insertMany(TypeConverter::fromLegacy(array_values($a)), $this->convertWriteConcernOptions($options));
     if (!$result->isAcknowledged()) {
         return true;
     }
     return ['connectionId' => 0, 'n' => 0, 'syncMillis' => 0, 'writtenTo' => null, 'err' => null, 'errmsg' => null];
 }
Example #4
0
 /**
  * Insert a new record into the database.
  *
  * @param  array  $values
  * @return bool
  */
 public function insert(array $values)
 {
     // Since every insert gets treated like a batch insert, we will have to detect
     // if the user is inserting a single document or an array of documents.
     $batch = true;
     foreach ($values as $value) {
         // As soon as we find a value that is not an array we assume the user is
         // inserting a single document.
         if (!is_array($value)) {
             $batch = false;
             break;
         }
     }
     if (!$batch) {
         $values = [$values];
     }
     // Batch insert
     $result = $this->collection->insertMany($values);
     return $result->isAcknowledged();
 }
 /**
  * Inserts multiple documents into this collection
  * @link http://www.php.net/manual/en/mongocollection.batchinsert.php
  * @param array $a An array of arrays.
  * @param array $options Options for the inserts.
  * @throws MongoCursorException
  * @return mixed f "safe" is set, returns an associative array with the status of the inserts ("ok") and any error that may have occured ("err"). Otherwise, returns TRUE if the batch insert was successfully sent, FALSE otherwise.
  */
 public function batchInsert(array $a, array $options = array())
 {
     return $this->collection->insertMany($a, $options);
 }
 public function initializeDatabases($data, $test)
 {
     $collectionsToDrop = ['fs.files', 'fs.chunks', 'expected.files', 'expected.chunks'];
     $data = $this->fixTypes($data, true);
     foreach ($collectionsToDrop as $collectionName) {
         $collection = new Collection($this->manager, sprintf("%s.%s", $this->getDatabaseName(), $collectionName));
         $collection->drop();
     }
     if (isset($data['files']) && count($data['files']) > 0) {
         $filesCollection = new Collection($this->manager, sprintf("%s.%s", $this->getDatabaseName(), "fs.files"));
         $filesCollection->insertMany($data['files']);
         $expectedFilesCollection = new Collection($this->manager, sprintf("%s.%s", $this->getDatabaseName(), "expected.files"));
         $expectedFilesCollection->insertMany($data['files']);
         $this->collections['expected.files'] = $expectedFilesCollection;
     }
     if (isset($data['chunks']) && count($data['chunks']) > 0) {
         $chunksCollection = new Collection($this->manager, sprintf("%s.%s", $this->getDatabaseName(), "fs.chunks"));
         $chunksCollection->insertMany($data['chunks']);
         $expectedChunksCollection = new Collection($this->manager, sprintf("%s.%s", $this->getDatabaseName(), "expected.chunks"));
         $expectedChunksCollection->insertMany($data['chunks']);
         $this->collections['expected.chunks'] = $expectedChunksCollection;
     }
     if (isset($test['arrange'])) {
         foreach ($test['arrange']['data'] as $cmd) {
             foreach ($cmd as $key => $value) {
                 if (isset($this->commands[$key])) {
                     $collection = new Collection($this->manager, sprintf("%s.%s", $this->getDatabaseName(), $cmd[$key]));
                     $this->commands[$key]($collection, $this->fixTypes($cmd, true));
                 }
             }
         }
     }
 }
 protected function insertMany(Collection $collection, $models)
 {
     $result = $collection->insertMany($models);
     $result->getInsertedIds();
 }