extractIdFromInsertedDocument() public static method

This function is used when BulkWrite::insert() does not return a generated ID, which means that the ID should be fetched from an array offset, public property, or in the data returned by bsonSerialize().
See also: https://jira.mongodb.org/browse/PHPC-382
public static extractIdFromInsertedDocument ( array | object $document ) : mixed
$document array | object Inserted document
return mixed
Example #1
0
 /**
  * Execute the operation.
  *
  * @see Executable::execute()
  *
  * @param Server $server
  *
  * @return InsertOneResult
  */
 public function execute(Server $server)
 {
     $options = [];
     if (isset($this->options['bypassDocumentValidation']) && Functions::serverSupportsFeature($server, self::$wireVersionForDocumentLevelValidation)) {
         $options['bypassDocumentValidation'] = $this->options['bypassDocumentValidation'];
     }
     $bulk = new Bulk($options);
     $insertedId = $bulk->insert($this->document);
     if ($insertedId === null) {
         $insertedId = Functions::extractIdFromInsertedDocument($this->document);
     }
     $writeConcern = isset($this->options['writeConcern']) ? $this->options['writeConcern'] : null;
     $writeResult = $server->executeBulkWrite($this->databaseName . '.' . $this->collectionName, $bulk, $writeConcern);
     return new InsertOneResult($writeResult, $insertedId);
 }
Example #2
0
 /**
  * Execute the operation.
  *
  * @see Executable::execute()
  *
  * @param Server $server
  *
  * @return BulkWriteResult
  */
 public function execute(Server $server)
 {
     $options = ['ordered' => $this->options['ordered']];
     if (isset($this->options['bypassDocumentValidation']) && Functions::serverSupportsFeature($server, self::$wireVersionForDocumentLevelValidation)) {
         $options['bypassDocumentValidation'] = $this->options['bypassDocumentValidation'];
     }
     $bulk = new Bulk($options);
     $insertedIds = [];
     foreach ($this->operations as $i => $operation) {
         $type = key($operation);
         $args = current($operation);
         switch ($type) {
             case self::DELETE_MANY:
             case self::DELETE_ONE:
                 $bulk->delete($args[0], $args[1]);
                 break;
             case self::INSERT_ONE:
                 $insertedId = $bulk->insert($args[0]);
                 if ($insertedId !== null) {
                     $insertedIds[$i] = $insertedId;
                 } else {
                     $insertedIds[$i] = Functions::extractIdFromInsertedDocument($args[0]);
                 }
                 break;
             case self::REPLACE_ONE:
             case self::UPDATE_MANY:
             case self::UPDATE_ONE:
                 $bulk->update($args[0], $args[1], $args[2]);
         }
     }
     $writeConcern = isset($this->options['writeConcern']) ? $this->options['writeConcern'] : null;
     $writeResult = $server->executeBulkWrite($this->databaseName . '.' . $this->collectionName, $bulk, $writeConcern);
     return new BulkWriteResult($writeResult, $insertedIds);
 }