Commit 816de8df authored by Jens Segers's avatar Jens Segers

Adding pull support

parent 7471a0a7
......@@ -252,6 +252,13 @@ Add one or more items to an array.
User::where('name', 'John')->push('items', 'boots');
User::where('name', 'John')->push('items', array('sword', 'shield'));
***Pull***
Remove one or more values from an array.
User::where('name', 'John')->pull('items', 'boots');
User::where('name', 'John')->pull('items', array('sword', 'shield'));
### Query Caching
You may easily cache the results of a query using the remember method:
......
......@@ -412,9 +412,9 @@ class Builder extends \Illuminate\Database\Query\Builder {
}
/**
* Append a value to an array.
* Append one or more values to an array.
*
* @param string $column
* @param mixed $column
* @param mixed $value
* @return int
*/
......@@ -426,6 +426,7 @@ class Builder extends \Illuminate\Database\Query\Builder {
}
else if (is_array($value))
{
// $pushAll depricated
$query = array('$push' => array($column => array('$each' => $value)));
}
else
......@@ -436,6 +437,31 @@ class Builder extends \Illuminate\Database\Query\Builder {
return $this->performUpdate($query);
}
/**
* Remove one or more values from an array.
*
* @param mixed $column
* @param mixed $value
* @return int
*/
public function pull($column, $value = null)
{
if (is_array($column))
{
$query = array('$pull' => $column);
}
else if (is_array($value))
{
$query = array('$pullAll' => array($column => $value));
}
else
{
$query = array('$pull' => array($column => $value));
}
return $this->performUpdate($query);
}
/**
* Get a new instance of the query builder.
*
......
......@@ -128,4 +128,24 @@ class QueryTest extends PHPUnit_Framework_TestCase {
$this->assertEquals('tag4', $user['tags'][3]);
}
public function testPull()
{
$user = array('name' => 'John Doe', 'tags' => array('tag1', 'tag2', 'tag3', 'tag4'));
$id = DB::collection('users')->insertGetId($user);
DB::collection('users')->where('_id', $id)->pull('tags', 'tag3');
$user = DB::collection('users')->find($id);
$this->assertTrue(is_array($user['tags']));
$this->assertEquals(3, count($user['tags']));
$this->assertEquals('tag4', $user['tags'][2]);
DB::collection('users')->where('_id', $id)->pull('tags', array('tag2', 'tag4'));
$user = DB::collection('users')->find($id);
$this->assertTrue(is_array($user['tags']));
$this->assertEquals(1, count($user['tags']));
$this->assertEquals('tag1', $user['tags'][0]);
}
}
\ 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