Skip to content

PHPLIB-59 and PHPLIB-60: CRUD results and insertMany() #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 17, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 30 additions & 3 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -406,11 +406,10 @@ public function bulkWrite(array $bulk, array $options = array())
* Inserts the provided document
*
* @see http://docs.mongodb.org/manual/reference/command/insert/
* @see Collection::getWriteOptions() for supported $options
*
* @param array $document The document to insert
* @param array $options Additional options
* @return InsertResult
* @return InsertOneResult
*/
public function insertOne(array $document)
{
Expand All @@ -420,7 +419,35 @@ public function insertOne(array $document)
$id = $bulk->insert($document);
$wr = $this->manager->executeBulkWrite($this->ns, $bulk, $this->wc);

return new InsertResult($wr, $id);
return new InsertOneResult($wr, $id);
}

/**
* Inserts the provided documents
*
* @see http://docs.mongodb.org/manual/reference/command/insert/
*
* @param array $documents The documents to insert
* @return InsertManyResult
*/
public function insertMany(array $documents)
{
$options = array_merge($this->getWriteOptions());

$bulk = new BulkWrite($options["ordered"]);
$insertedIds = array();

foreach ($documents as $i => $document) {
$insertedId = $bulk->insert($document);

if ($insertedId !== null) {
$insertedIds[$i] = $insertedId;
}
}

$writeResult = $this->manager->executeBulkWrite($this->ns, $bulk, $this->wc);

return new InsertManyResult($writeResult, $insertedIds);
}

/**
Expand Down
37 changes: 33 additions & 4 deletions src/DeleteResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,46 @@

use MongoDB\Driver\WriteResult;

/**
* Result class for a delete operation.
*/
class DeleteResult
{
protected $wr;
private $writeResult;

public function __construct(WriteResult $wr)
/**
* Constructor.
*
* @param WriteResult $writeResult
*/
public function __construct(WriteResult $writeResult)
{
$this->wr = $wr;
$this->writeResult = $writeResult;
}

/**
* Return the number of documents that were deleted.
*
* This value is undefined if the write was not acknowledged.
*
* @see UpdateResult::isAcknowledged()
* @return integer
*/
public function getDeletedCount()
{
return $this->wr->getDeletedCount();
return $this->writeResult->getDeletedCount();
}

/**
* Return whether this delete was acknowledged by the server.
*
* If the delete was not acknowledged, other fields from the WriteResult
* (e.g. deletedCount) will be undefined.
*
* @return boolean
*/
public function isAcknowledged()
{
return $this->writeResult->isAcknowledged();
}
}
66 changes: 66 additions & 0 deletions src/InsertManyResult.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace MongoDB;

use MongoDB\Driver\WriteResult;

/**
* Result class for a multi-document write operation.
*/
class InsertManyResult
{
private $writeResult;
private $insertedIds;

/**
* Constructor.
*
* @param WriteResult $writeResult
* @param array $insertedIds
*/
public function __construct(WriteResult $writeResult, array $insertedIds = array())
{
$this->writeResult = $writeResult;
$this->insertedIds = $insertedIds;
}

/**
* Return the number of documents that were inserted.
*
* This value is undefined if the write was not acknowledged.
*
* @see InsertManyResult::isAcknowledged()
* @return integer
*/
public function getInsertedCount()
{
return $this->writeResult->getInsertedCount();
}

/**
* 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.
*
* @return array
*/
public function getInsertedIds()
{
return $this->insertedIds;
}

/**
* Return whether this insert result was acknowledged by the server.
*
* If the insert was not acknowledged, other fields from the WriteResult
* (e.g. insertedCount) will be undefined.
*
* @return boolean
*/
public function isAcknowledged()
{
return $this->writeResult->isAcknowledged();
}
}
66 changes: 66 additions & 0 deletions src/InsertOneResult.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace MongoDB;

use BSON\ObjectId;
use MongoDB\Driver\WriteResult;

/**
* Result class for a single-document insert operation.
*/
class InsertOneResult
{
private $writeResult;
private $insertedId;

/**
* Constructor.
*
* @param WriteResult $writeResult
* @param ObjectId $insertedId
*/
public function __construct(WriteResult $writeResult, ObjectId $insertedId = null)
{
$this->writeResult = $writeResult;
$this->insertedId = $insertedId;
}

/**
* Return the number of documents that were inserted.
*
* This value is undefined if the write was not acknowledged.
*
* @see InsertOneResult::isAcknowledged()
* @return integer
*/
public function getInsertedCount()
{
return $this->writeResult->getInsertedCount();
}

/**
* 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.
*
* @return ObjectId|null
*/
public function getInsertedId()
{
return $this->insertedId;
}

/**
* Return whether this insert was acknowledged by the server.
*
* If the insert was not acknowledged, other fields from the WriteResult
* (e.g. insertedCount) will be undefined.
*
* @return boolean
*/
public function isAcknowledged()
{
return $this->writeResult->isAcknowledged();
}
}
22 changes: 0 additions & 22 deletions src/InsertResult.php

This file was deleted.

59 changes: 53 additions & 6 deletions src/UpdateResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,76 @@

namespace MongoDB;

use BSON\ObjectId;
use MongoDB\Driver\WriteResult;

/**
* Result class for an update operation.
*/
class UpdateResult
{
protected $wr;
private $writeResult;

public function __construct(WriteResult $wr)
/**
* Constructor.
*
* @param WriteResult $writeResult
*/
public function __construct(WriteResult $writeResult)
{
$this->wr = $wr;
$this->writeResult = $writeResult;
}

/**
* Return the number of documents that were matched by the filter.
*
* This value is undefined if the write was not acknowledged.
*
* @see UpdateResult::isAcknowledged()
* @return integer
*/
public function getMatchedCount()
{
return $this->wr->getMatchedCount();
return $this->writeResult->getMatchedCount();
}

/**
* Return the number of documents that were modified.
*
* This value is undefined if the write was not acknowledged.
*
* @see UpdateResult::isAcknowledged()
* @return integer
*/
public function getModifiedCount()
{
return $this->wr->getModifiedCount();
return $this->writeResult->getModifiedCount();
}

/**
* Return the ID of the document inserted by an upsert operation.
*
* This value is undefined if an upsert did not take place.
*
* @return ObjectId|null
*/
public function getUpsertedId()
{
return $this->wr->getUpsertedIds()[0];
foreach ($this->writeResult->getUpsertedIds() as $id) {
return $id;
}
}

/**
* Return whether this update was acknowledged by the server.
*
* If the update was not acknowledged, other fields from the WriteResult
* (e.g. matchedCount) will be undefined.
*
* @return boolean
*/
public function isAcknowledged()
{
return $this->writeResult->isAcknowledged();
}
}
2 changes: 1 addition & 1 deletion tests/CollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function testInsertAndRetrieve() {
for($i=0; $i<10;$i++) {
$user = createUser($this->faker);
$result = $collection->insertOne($user);
$this->assertInstanceOf('MongoDB\InsertResult', $result);
$this->assertInstanceOf('MongoDB\InsertOneResult', $result);
$this->assertInstanceOf('BSON\ObjectId', $result->getInsertedId());
$this->assertEquals(24, strlen($result->getInsertedId()));

Expand Down