Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
M
mongo-php-library
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
sinan
mongo-php-library
Commits
27543788
Commit
27543788
authored
Mar 17, 2015
by
Jeremy Mikola
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #3
parents
a3f3e85c
af72fa5f
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
249 additions
and
36 deletions
+249
-36
Collection.php
src/Collection.php
+30
-3
DeleteResult.php
src/DeleteResult.php
+33
-4
InsertManyResult.php
src/InsertManyResult.php
+66
-0
InsertOneResult.php
src/InsertOneResult.php
+66
-0
InsertResult.php
src/InsertResult.php
+0
-22
UpdateResult.php
src/UpdateResult.php
+53
-6
CollectionTest.php
tests/CollectionTest.php
+1
-1
No files found.
src/Collection.php
View file @
27543788
...
@@ -406,11 +406,10 @@ class Collection
...
@@ -406,11 +406,10 @@ class Collection
* Inserts the provided document
* Inserts the provided document
*
*
* @see http://docs.mongodb.org/manual/reference/command/insert/
* @see http://docs.mongodb.org/manual/reference/command/insert/
* @see Collection::getWriteOptions() for supported $options
*
*
* @param array $document The document to insert
* @param array $document The document to insert
* @param array $options Additional options
* @param array $options Additional options
* @return InsertResult
* @return Insert
One
Result
*/
*/
public
function
insertOne
(
array
$document
)
public
function
insertOne
(
array
$document
)
{
{
...
@@ -420,7 +419,35 @@ class Collection
...
@@ -420,7 +419,35 @@ 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
);
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
);
}
}
/**
/**
...
...
src/DeleteResult.php
View file @
27543788
...
@@ -4,17 +4,46 @@ namespace MongoDB;
...
@@ -4,17 +4,46 @@ namespace MongoDB;
use
MongoDB\Driver\WriteResult
;
use
MongoDB\Driver\WriteResult
;
/**
* Result class for a delete operation.
*/
class
DeleteResult
class
DeleteResult
{
{
pr
otected
$wr
;
pr
ivate
$writeResult
;
public
function
__construct
(
WriteResult
$wr
)
/**
* Constructor.
*
* @param WriteResult $writeResult
*/
public
function
__construct
(
WriteResult
$writeResult
)
{
{
$this
->
wr
=
$wr
;
$this
->
wr
iteResult
=
$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
()
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
();
}
}
}
}
src/InsertManyResult.php
0 → 100644
View file @
27543788
<?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
();
}
}
src/InsertOneResult.php
0 → 100644
View file @
27543788
<?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
();
}
}
src/InsertResult.php
deleted
100644 → 0
View file @
a3f3e85c
<?php
namespace
MongoDB
;
use
BSON\ObjectId
;
use
MongoDB\Driver\WriteResult
;
class
InsertResult
{
protected
$wr
;
public
function
__construct
(
WriteResult
$wr
,
ObjectId
$id
=
null
)
{
$this
->
wr
=
$wr
;
$this
->
id
=
$id
;
}
public
function
getInsertedId
()
{
return
$this
->
id
;
}
}
src/UpdateResult.php
View file @
27543788
...
@@ -2,29 +2,76 @@
...
@@ -2,29 +2,76 @@
namespace
MongoDB
;
namespace
MongoDB
;
use
BSON\ObjectId
;
use
MongoDB\Driver\WriteResult
;
use
MongoDB\Driver\WriteResult
;
/**
* Result class for an update operation.
*/
class
UpdateResult
class
UpdateResult
{
{
pr
otected
$wr
;
pr
ivate
$writeResult
;
public
function
__construct
(
WriteResult
$wr
)
/**
* Constructor.
*
* @param WriteResult $writeResult
*/
public
function
__construct
(
WriteResult
$writeResult
)
{
{
$this
->
wr
=
$wr
;
$this
->
wr
iteResult
=
$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
()
public
function
getMatchedCount
()
{
{
return
$this
->
wr
->
getMatchedCount
();
return
$this
->
wr
iteResult
->
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
()
public
function
getModifiedCount
()
{
{
return
$this
->
wr
->
getModifiedCount
();
return
$this
->
wr
iteResult
->
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
()
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
();
}
}
}
}
tests/CollectionTest.php
View file @
27543788
...
@@ -21,7 +21,7 @@ class CollectionTest extends PHPUnit_Framework_TestCase {
...
@@ -21,7 +21,7 @@ class CollectionTest extends PHPUnit_Framework_TestCase {
for
(
$i
=
0
;
$i
<
10
;
$i
++
)
{
for
(
$i
=
0
;
$i
<
10
;
$i
++
)
{
$user
=
createUser
(
$this
->
faker
);
$user
=
createUser
(
$this
->
faker
);
$result
=
$collection
->
insertOne
(
$user
);
$result
=
$collection
->
insertOne
(
$user
);
$this
->
assertInstanceOf
(
'MongoDB\InsertResult'
,
$result
);
$this
->
assertInstanceOf
(
'MongoDB\Insert
One
Result'
,
$result
);
$this
->
assertInstanceOf
(
'BSON\ObjectId'
,
$result
->
getInsertedId
());
$this
->
assertInstanceOf
(
'BSON\ObjectId'
,
$result
->
getInsertedId
());
$this
->
assertEquals
(
24
,
strlen
(
$result
->
getInsertedId
()));
$this
->
assertEquals
(
24
,
strlen
(
$result
->
getInsertedId
()));
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment