Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 400 and 401]

   1  <?php
   2  /*
   3   * Copyright 2015-present MongoDB, Inc.
   4   *
   5   * Licensed under the Apache License, Version 2.0 (the "License");
   6   * you may not use this file except in compliance with the License.
   7   * You may obtain a copy of the License at
   8   *
   9   *   https://www.apache.org/licenses/LICENSE-2.0
  10   *
  11   * Unless required by applicable law or agreed to in writing, software
  12   * distributed under the License is distributed on an "AS IS" BASIS,
  13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14   * See the License for the specific language governing permissions and
  15   * limitations under the License.
  16   */
  17  
  18  namespace MongoDB;
  19  
  20  use MongoDB\Driver\WriteResult;
  21  use MongoDB\Exception\BadMethodCallException;
  22  
  23  /**
  24   * Result class for a multi-document insert operation.
  25   */
  26  class InsertManyResult
  27  {
  28      /** @var WriteResult */
  29      private $writeResult;
  30  
  31      /** @var array */
  32      private $insertedIds;
  33  
  34      /** @var boolean */
  35      private $isAcknowledged;
  36  
  37      public function __construct(WriteResult $writeResult, array $insertedIds)
  38      {
  39          $this->writeResult = $writeResult;
  40          $this->insertedIds = $insertedIds;
  41          $this->isAcknowledged = $writeResult->isAcknowledged();
  42      }
  43  
  44      /**
  45       * Return the number of documents that were inserted.
  46       *
  47       * This method should only be called if the write was acknowledged.
  48       *
  49       * @see InsertManyResult::isAcknowledged()
  50       * @return integer|null
  51       * @throws BadMethodCallException is the write result is unacknowledged
  52       */
  53      public function getInsertedCount()
  54      {
  55          if ($this->isAcknowledged) {
  56              return $this->writeResult->getInsertedCount();
  57          }
  58  
  59          throw BadMethodCallException::unacknowledgedWriteResultAccess(__METHOD__);
  60      }
  61  
  62      /**
  63       * Return a map of the inserted documents' IDs.
  64       *
  65       * The index of each ID in the map corresponds to each document's position
  66       * in the bulk operation. If a document had an ID prior to inserting (i.e.
  67       * the driver did not generate an ID), the index will contain its "_id"
  68       * field value. Any driver-generated ID will be a MongoDB\BSON\ObjectId
  69       * instance.
  70       *
  71       * @return array
  72       */
  73      public function getInsertedIds()
  74      {
  75          return $this->insertedIds;
  76      }
  77  
  78      /**
  79       * Return whether this insert result was acknowledged by the server.
  80       *
  81       * If the insert was not acknowledged, other fields from the WriteResult
  82       * (e.g. insertedCount) will be undefined.
  83       *
  84       * @return boolean
  85       */
  86      public function isAcknowledged()
  87      {
  88          return $this->writeResult->isAcknowledged();
  89      }
  90  }