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 single-document insert operation.
  25   */
  26  class InsertOneResult
  27  {
  28      /** @var WriteResult */
  29      private $writeResult;
  30  
  31      /** @var mixed */
  32      private $insertedId;
  33  
  34      /** @var boolean */
  35      private $isAcknowledged;
  36  
  37      /**
  38       * @param mixed $insertedId
  39       */
  40      public function __construct(WriteResult $writeResult, $insertedId)
  41      {
  42          $this->writeResult = $writeResult;
  43          $this->insertedId = $insertedId;
  44          $this->isAcknowledged = $writeResult->isAcknowledged();
  45      }
  46  
  47      /**
  48       * Return the number of documents that were inserted.
  49       *
  50       * This method should only be called if the write was acknowledged.
  51       *
  52       * @see InsertOneResult::isAcknowledged()
  53       * @return integer|null
  54       * @throws BadMethodCallException is the write result is unacknowledged
  55       */
  56      public function getInsertedCount()
  57      {
  58          if ($this->isAcknowledged) {
  59              return $this->writeResult->getInsertedCount();
  60          }
  61  
  62          throw BadMethodCallException::unacknowledgedWriteResultAccess(__METHOD__);
  63      }
  64  
  65      /**
  66       * Return the inserted document's ID.
  67       *
  68       * If the document had an ID prior to inserting (i.e. the driver did not
  69       * need to generate an ID), this will contain its "_id". Any
  70       * driver-generated ID will be a MongoDB\BSON\ObjectId instance.
  71       *
  72       * @return mixed
  73       */
  74      public function getInsertedId()
  75      {
  76          return $this->insertedId;
  77      }
  78  
  79      /**
  80       * Return whether this insert was acknowledged by the server.
  81       *
  82       * If the insert was not acknowledged, other fields from the WriteResult
  83       * (e.g. insertedCount) will be undefined.
  84       *
  85       * If the insert was not acknowledged, other fields from the WriteResult
  86       * (e.g. insertedCount) will be undefined and their getter methods should
  87       * not be invoked.
  88       *
  89       * @return boolean
  90       */
  91      public function isAcknowledged()
  92      {
  93          return $this->writeResult->isAcknowledged();
  94      }
  95  }