Skip to content

Commit 719fd25

Browse files
committed
PHPLIB-93: Insert result classes should always track IDs
1 parent 619c27d commit 719fd25

File tree

3 files changed

+20
-14
lines changed

3 files changed

+20
-14
lines changed

src/Collection.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -961,7 +961,7 @@ public function getWriteOptions()
961961
*
962962
* @see http://docs.mongodb.org/manual/reference/command/insert/
963963
*
964-
* @param array $documents The documents to insert
964+
* @param array[]|object[] $documents The documents to insert
965965
* @return InsertManyResult
966966
*/
967967
public function insertMany(array $documents)
@@ -976,6 +976,8 @@ public function insertMany(array $documents)
976976

977977
if ($insertedId !== null) {
978978
$insertedIds[$i] = $insertedId;
979+
} else {
980+
$insertedIds[$i] = is_array($document) ? $document['_id'] : $document->_id;
979981
}
980982
}
981983

@@ -989,18 +991,21 @@ public function insertMany(array $documents)
989991
*
990992
* @see http://docs.mongodb.org/manual/reference/command/insert/
991993
*
992-
* @param array $document The document to insert
993-
* @param array $options Additional options
994+
* @param array|object $document The document to insert
994995
* @return InsertOneResult
995996
*/
996-
public function insertOne(array $document)
997+
public function insertOne($document)
997998
{
998999
$options = array_merge($this->getWriteOptions());
9991000

10001001
$bulk = new BulkWrite($options["ordered"]);
10011002
$id = $bulk->insert($document);
10021003
$wr = $this->manager->executeBulkWrite($this->ns, $bulk, $this->wc);
10031004

1005+
if ($id === null) {
1006+
$id = is_array($document) ? $document['_id'] : $document->_id;
1007+
}
1008+
10041009
return new InsertOneResult($wr, $id);
10051010
}
10061011

src/InsertManyResult.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class InsertManyResult
1616
* Constructor.
1717
*
1818
* @param WriteResult $writeResult
19-
* @param array $insertedIds
19+
* @param mixed[] $insertedIds
2020
*/
2121
public function __construct(WriteResult $writeResult, array $insertedIds = array())
2222
{
@@ -41,10 +41,11 @@ public function getInsertedCount()
4141
* Return the map of inserted IDs that were generated by the driver.
4242
*
4343
* The index of each ID in the map corresponds to the document's position
44-
* in bulk operation. If an inserted document already had an ID (e.g. it was
45-
* generated by the application), it will not be present in this map.
44+
* in bulk operation. If the document already an ID prior to insertion (i.e.
45+
* the driver did not need to generate an ID), this will contain its "_id".
46+
* Any driver-generated ID will be an MongoDB\Driver\ObjectID instance.
4647
*
47-
* @return array
48+
* @return mixed[]
4849
*/
4950
public function getInsertedIds()
5051
{

src/InsertOneResult.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace MongoDB;
44

5-
use BSON\ObjectId;
65
use MongoDB\Driver\WriteResult;
76

87
/**
@@ -17,9 +16,9 @@ class InsertOneResult
1716
* Constructor.
1817
*
1918
* @param WriteResult $writeResult
20-
* @param ObjectId $insertedId
19+
* @param mixed $insertedId
2120
*/
22-
public function __construct(WriteResult $writeResult, ObjectId $insertedId = null)
21+
public function __construct(WriteResult $writeResult, $insertedId)
2322
{
2423
$this->writeResult = $writeResult;
2524
$this->insertedId = $insertedId;
@@ -41,10 +40,11 @@ public function getInsertedCount()
4140
/**
4241
* Return the inserted ID that was generated by the driver.
4342
*
44-
* If the inserted document already had an ID (e.g. it was generated by the
45-
* application), this will be null.
43+
* If the document already an ID prior to insertion (i.e. the driver did not
44+
* need to generate an ID), this will contain its "_id". Any
45+
* driver-generated ID will be an MongoDB\Driver\ObjectID instance.
4646
*
47-
* @return ObjectId|null
47+
* @return mixed
4848
*/
4949
public function getInsertedId()
5050
{

0 commit comments

Comments
 (0)