Skip to content

Commit da84ba5

Browse files
committed
PHP-1309: Collection::replaceOne()
1 parent 13ded9a commit da84ba5

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

examples/write.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,15 @@
8181
foreach($result as $document) {
8282
var_dump($document);
8383
}
84+
$result = $collection->replaceOne(
85+
array("nick" => "Bobby Fischer"),
86+
array("name" => "Magnus Carlsen", "nick" => "unknown", "citizen" => "Norway")
87+
);
88+
printf("Replaced: %d (out of expected 1), verify Bobby has been replaced with Magnus\n", $result->getNumModified());
89+
$result = $collection->find();
90+
foreach($result as $document) {
91+
var_dump($document);
92+
}
8493

8594
$result = $collection->deleteMany(array("citizen" => "Iceland"));
8695
printf("Deleted: %d (out of expected 3)\n", $result->getNumRemoved());

src/Collection.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,9 +209,18 @@ function deleteOne(array $filter) { /* {{{ */
209209
function deleteMany(array $filter) { /* {{{ */
210210
return $this->_writeSingle($filter, self::DELETE, array("limit" => 0));
211211
} /* }}} */
212-
function updateOne(array $filter, $update, array $options = array()) { /* {{{ */
212+
function updateOne(array $filter, array $update, array $options = array()) { /* {{{ */
213+
if (key($update)[0] != '$') {
214+
throw new \RuntimeException("First key in \$update must be a \$operator");
215+
}
213216
return $this->_writeSingle($filter, self::UPDATE, $options, $update);
214217
} /* }}} */
218+
function replaceOne(array $filter, array $update, array $options = array()) { /* {{{ */
219+
if (key($update)[0] == '$') {
220+
throw new \RuntimeException("First key in \$update must NOT be a \$operator");
221+
}
222+
return $this->_writeSingle($filter, self::UPDATE, $options, array('$set' => $update));
223+
} /* }}} */
215224
function updateMany(array $filter, $update, array $options = array()) { /* {{{ */
216225
return $this->_writeSingle($filter, self::UPDATE, $options + array("limit" => 0), $update);
217226
} /* }}} */

0 commit comments

Comments
 (0)