コード例 #1
0
ファイル: MongodbStorage.php プロジェクト: makasim/yadm
 /**
  * @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;
 }
コード例 #2
0
 /**
  * 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];
 }
コード例 #3
0
 /**
  * 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];
 }
コード例 #4
0
ファイル: Builder.php プロジェクト: dotuancd/laravel-mongodb
 /**
  * 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();
 }
コード例 #5
0
 /**
  * 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);
 }
コード例 #6
0
 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));
                 }
             }
         }
     }
 }
コード例 #7
0
 protected function insertMany(Collection $collection, $models)
 {
     $result = $collection->insertMany($models);
     $result->getInsertedIds();
 }