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
*
* @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
*/
public function insertMany(array $documents)
......@@ -976,6 +976,8 @@ class Collection
if ($insertedId !== null) {
$insertedIds[$i] = $insertedId;
} else {
$insertedIds[$i] = is_array($document) ? $document['_id'] : $document->_id;
}
}
......@@ -989,11 +991,10 @@ class Collection
*
* @see http://docs.mongodb.org/manual/reference/command/insert/
*
* @param array $document The document to insert
* @param array $options Additional options
* @param array|object $document The document to insert
* @return InsertOneResult
*/
public function insertOne(array $document)
public function insertOne($document)
{
$options = array_merge($this->getWriteOptions());
......@@ -1001,6 +1002,10 @@ class Collection
$id = $bulk->insert($document);
$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);
}
......
......@@ -16,7 +16,7 @@ class InsertManyResult
* Constructor.
*
* @param WriteResult $writeResult
* @param array $insertedIds
* @param mixed[] $insertedIds
*/
public function __construct(WriteResult $writeResult, array $insertedIds = array())
{
......@@ -41,10 +41,11 @@ class InsertManyResult
* 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
* in bulk operation. If an inserted document already had an ID (e.g. it was
* generated by the application), it will not be present in this map.
* in bulk operation. If the document already an ID prior to insertion (i.e.
* 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()
{
......
......@@ -2,7 +2,6 @@
namespace MongoDB;
use BSON\ObjectId;
use MongoDB\Driver\WriteResult;
/**
......@@ -17,9 +16,9 @@ class InsertOneResult
* Constructor.
*
* @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->insertedId = $insertedId;
......@@ -41,10 +40,11 @@ class InsertOneResult
/**
* 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
* application), this will be null.
* If the document already an ID prior to insertion (i.e. 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 ObjectId|null
* @return mixed
*/
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