Skip to content

Commit e651f0e

Browse files
committed
Added options to update method to allow upsert, fixes #31
1 parent dcaec6e commit e651f0e

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

Diff for: src/Jenssegers/Mongodb/Builder.php

+10-4
Original file line numberDiff line numberDiff line change
@@ -323,9 +323,9 @@ public function insertGetId(array $values, $sequence = null)
323323
* @param array $values
324324
* @return int
325325
*/
326-
public function update(array $values)
326+
public function update(array $values, array $options = array())
327327
{
328-
return $this->performUpdate(array('$set' => $values));
328+
return $this->performUpdate(array('$set' => $values), $options);
329329
}
330330

331331
/**
@@ -520,9 +520,15 @@ public function newQuery()
520520
* @param array $query
521521
* @return int
522522
*/
523-
protected function performUpdate($query)
523+
protected function performUpdate($query, array $options = array())
524524
{
525-
$result = $this->collection->update($this->compileWheres(), $query, array('multiple' => true));
525+
// Default options
526+
$default = array('multiple' => true);
527+
528+
// Merge options and override default options
529+
$options = array_merge($default, $options);
530+
531+
$result = $this->collection->update($this->compileWheres(), $query, $options);
526532

527533
if (1 == (int) $result['ok'])
528534
{

Diff for: tests/QueryBuilderTest.php

+11
Original file line numberDiff line numberDiff line change
@@ -372,4 +372,15 @@ public function testSubdocumentAggregate()
372372
$this->assertEquals(16.25, DB::collection('items')->avg('amount.hidden'));
373373
}
374374

375+
public function testUpsert()
376+
{
377+
DB::collection('items')->where('name', 'knife')
378+
->update(
379+
array('amount' => 1),
380+
array('upsert' => true)
381+
);
382+
383+
$this->assertEquals(1, DB::collection('items')->count());
384+
}
385+
375386
}

0 commit comments

Comments
 (0)