Commit 719fd253 authored by Jeremy Mikola's avatar Jeremy Mikola

PHPLIB-93: Insert result classes should always track IDs

parent 619c27d8
...@@ -961,7 +961,7 @@ class Collection ...@@ -961,7 +961,7 @@ class Collection
* *
* @see http://docs.mongodb.org/manual/reference/command/insert/ * @see http://docs.mongodb.org/manual/reference/command/insert/
* *
* @param array $documents The documents to insert * @param array[]|object[] $documents The documents to insert
* @return InsertManyResult * @return InsertManyResult
*/ */
public function insertMany(array $documents) public function insertMany(array $documents)
...@@ -976,6 +976,8 @@ class Collection ...@@ -976,6 +976,8 @@ class Collection
if ($insertedId !== null) { if ($insertedId !== null) {
$insertedIds[$i] = $insertedId; $insertedIds[$i] = $insertedId;
} else {
$insertedIds[$i] = is_array($document) ? $document['_id'] : $document->_id;
} }
} }
...@@ -989,11 +991,10 @@ class Collection ...@@ -989,11 +991,10 @@ class Collection
* *
* @see http://docs.mongodb.org/manual/reference/command/insert/ * @see http://docs.mongodb.org/manual/reference/command/insert/
* *
* @param array $document The document to insert * @param array|object $document The document to insert
* @param array $options Additional options
* @return InsertOneResult * @return InsertOneResult
*/ */
public function insertOne(array $document) public function insertOne($document)
{ {
$options = array_merge($this->getWriteOptions()); $options = array_merge($this->getWriteOptions());
...@@ -1001,6 +1002,10 @@ class Collection ...@@ -1001,6 +1002,10 @@ class Collection
$id = $bulk->insert($document); $id = $bulk->insert($document);
$wr = $this->manager->executeBulkWrite($this->ns, $bulk, $this->wc); $wr = $this->manager->executeBulkWrite($this->ns, $bulk, $this->wc);
if ($id === null) {
$id = is_array($document) ? $document['_id'] : $document->_id;
}
return new InsertOneResult($wr, $id); return new InsertOneResult($wr, $id);
} }
......
...@@ -16,7 +16,7 @@ class InsertManyResult ...@@ -16,7 +16,7 @@ class InsertManyResult
* Constructor. * Constructor.
* *
* @param WriteResult $writeResult * @param WriteResult $writeResult
* @param array $insertedIds * @param mixed[] $insertedIds
*/ */
public function __construct(WriteResult $writeResult, array $insertedIds = array()) public function __construct(WriteResult $writeResult, array $insertedIds = array())
{ {
...@@ -41,10 +41,11 @@ class InsertManyResult ...@@ -41,10 +41,11 @@ class InsertManyResult
* Return the map of inserted IDs that were generated by the driver. * Return the map of inserted IDs that were generated by the driver.
* *
* The index of each ID in the map corresponds to the document's position * The index of each ID in the map corresponds to the document's position
* in bulk operation. If an inserted document already had an ID (e.g. it was * in bulk operation. If the document already an ID prior to insertion (i.e.
* generated by the application), it will not be present in this map. * the driver did not need to generate an ID), this will contain its "_id".
* Any driver-generated ID will be an MongoDB\Driver\ObjectID instance.
* *
* @return array * @return mixed[]
*/ */
public function getInsertedIds() public function getInsertedIds()
{ {
......
...@@ -2,7 +2,6 @@ ...@@ -2,7 +2,6 @@
namespace MongoDB; namespace MongoDB;
use BSON\ObjectId;
use MongoDB\Driver\WriteResult; use MongoDB\Driver\WriteResult;
/** /**
...@@ -17,9 +16,9 @@ class InsertOneResult ...@@ -17,9 +16,9 @@ class InsertOneResult
* Constructor. * Constructor.
* *
* @param WriteResult $writeResult * @param WriteResult $writeResult
* @param ObjectId $insertedId * @param mixed $insertedId
*/ */
public function __construct(WriteResult $writeResult, ObjectId $insertedId = null) public function __construct(WriteResult $writeResult, $insertedId)
{ {
$this->writeResult = $writeResult; $this->writeResult = $writeResult;
$this->insertedId = $insertedId; $this->insertedId = $insertedId;
...@@ -41,10 +40,11 @@ class InsertOneResult ...@@ -41,10 +40,11 @@ class InsertOneResult
/** /**
* Return the inserted ID that was generated by the driver. * Return the inserted ID that was generated by the driver.
* *
* If the inserted document already had an ID (e.g. it was generated by the * If the document already an ID prior to insertion (i.e. the driver did not
* application), this will be null. * need to generate an ID), this will contain its "_id". Any
* driver-generated ID will be an MongoDB\Driver\ObjectID instance.
* *
* @return ObjectId|null * @return mixed
*/ */
public function getInsertedId() public function getInsertedId()
{ {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment