Commit e651f0ed authored by Jens Segers's avatar Jens Segers

Added options to update method to allow upsert, fixes #31

parent dcaec6eb
...@@ -323,9 +323,9 @@ class Builder extends \Illuminate\Database\Query\Builder { ...@@ -323,9 +323,9 @@ class Builder extends \Illuminate\Database\Query\Builder {
* @param array $values * @param array $values
* @return int * @return int
*/ */
public function update(array $values) public function update(array $values, array $options = array())
{ {
return $this->performUpdate(array('$set' => $values)); return $this->performUpdate(array('$set' => $values), $options);
} }
/** /**
...@@ -520,9 +520,15 @@ class Builder extends \Illuminate\Database\Query\Builder { ...@@ -520,9 +520,15 @@ class Builder extends \Illuminate\Database\Query\Builder {
* @param array $query * @param array $query
* @return int * @return int
*/ */
protected function performUpdate($query) protected function performUpdate($query, array $options = array())
{ {
$result = $this->collection->update($this->compileWheres(), $query, array('multiple' => true)); // Default options
$default = array('multiple' => true);
// Merge options and override default options
$options = array_merge($default, $options);
$result = $this->collection->update($this->compileWheres(), $query, $options);
if (1 == (int) $result['ok']) if (1 == (int) $result['ok'])
{ {
......
...@@ -372,4 +372,15 @@ class QueryBuilderTest extends PHPUnit_Framework_TestCase { ...@@ -372,4 +372,15 @@ class QueryBuilderTest extends PHPUnit_Framework_TestCase {
$this->assertEquals(16.25, DB::collection('items')->avg('amount.hidden')); $this->assertEquals(16.25, DB::collection('items')->avg('amount.hidden'));
} }
public function testUpsert()
{
DB::collection('items')->where('name', 'knife')
->update(
array('amount' => 1),
array('upsert' => true)
);
$this->assertEquals(1, DB::collection('items')->count());
}
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment