Commit b3497f2b authored by Jens Segers's avatar Jens Segers

Added support for push, fixes #20

parent b115d728
...@@ -243,6 +243,15 @@ Or you can access the internal object directly: ...@@ -243,6 +243,15 @@ Or you can access the internal object directly:
User::raw()->find(); User::raw()->find();
### MongoDB specific operations
***Push***
Add one or more items to an array.
User::where('name', 'John')->push('items', 'boots');
User::where('name', 'John')->push('items', array('sword', 'shield'));
### Query Caching ### Query Caching
You may easily cache the results of a query using the remember method: You may easily cache the results of a query using the remember method:
......
...@@ -303,16 +303,7 @@ class Builder extends \Illuminate\Database\Query\Builder { ...@@ -303,16 +303,7 @@ class Builder extends \Illuminate\Database\Query\Builder {
*/ */
public function update(array $values) public function update(array $values)
{ {
$update = array('$set' => $values); return $this->performUpdate(array('$set' => $values));
$result = $this->collection->update($this->compileWheres(), $update, array('multiple' => true));
if (1 == (int) $result['ok'])
{
return $result['n'];
}
return 0;
} }
/** /**
...@@ -420,6 +411,31 @@ class Builder extends \Illuminate\Database\Query\Builder { ...@@ -420,6 +411,31 @@ class Builder extends \Illuminate\Database\Query\Builder {
return $this->collection; return $this->collection;
} }
/**
* Append a value to an array.
*
* @param string $column
* @param mixed $value
* @return int
*/
public function push($column, $value = null)
{
if (is_array($column))
{
$query = array('$push' => $column);
}
else if (is_array($value))
{
$query = array('$push' => array($column => array('$each' => $value)));
}
else
{
$query = array('$push' => array($column => $value));
}
return $this->performUpdate($query);
}
/** /**
* Get a new instance of the query builder. * Get a new instance of the query builder.
* *
...@@ -430,6 +446,24 @@ class Builder extends \Illuminate\Database\Query\Builder { ...@@ -430,6 +446,24 @@ class Builder extends \Illuminate\Database\Query\Builder {
return new Builder($this->connection); return new Builder($this->connection);
} }
/**
* Perform update.
*
* @param array $query
* @return int
*/
protected function performUpdate($query)
{
$result = $this->collection->update($this->compileWheres(), $query, array('multiple' => true));
if (1 == (int) $result['ok'])
{
return $result['n'];
}
return 0;
}
/** /**
* Compile the where array * Compile the where array
* *
......
...@@ -101,4 +101,31 @@ class QueryTest extends PHPUnit_Framework_TestCase { ...@@ -101,4 +101,31 @@ class QueryTest extends PHPUnit_Framework_TestCase {
$this->assertInstanceOf('MongoCollection', $collection); $this->assertInstanceOf('MongoCollection', $collection);
} }
public function testPush()
{
$user = array('name' => 'John Doe', 'tags' => array());
$id = DB::collection('users')->insertGetId($user);
DB::collection('users')->where('_id', $id)->push('tags', 'tag1');
$user = DB::collection('users')->find($id);
$this->assertTrue(is_array($user['tags']));
$this->assertEquals(1, count($user['tags']));
$this->assertEquals('tag1', $user['tags'][0]);
DB::collection('users')->where('_id', $id)->push('tags', 'tag2');
$user = DB::collection('users')->find($id);
$this->assertTrue(is_array($user['tags']));
$this->assertEquals(2, count($user['tags']));
$this->assertEquals('tag2', $user['tags'][1]);
DB::collection('users')->where('_id', $id)->push('tags', array('tag3', 'tag4'));
$user = DB::collection('users')->find($id);
$this->assertTrue(is_array($user['tags']));
$this->assertEquals(4, count($user['tags']));
$this->assertEquals('tag4', $user['tags'][3]);
}
} }
\ 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