Commit 1e533493 authored by Jens Segers's avatar Jens Segers

Fixed push and pull behaviour for #41

parent 94b99a0f
...@@ -276,17 +276,17 @@ Or you can access the internal object directly: ...@@ -276,17 +276,17 @@ Or you can access the internal object directly:
**Push** **Push**
Add one or more items to an array. Add an items to an array.
DB::collection('users')->where('name', 'John')->push('items', 'boots'); DB::collection('users')->where('name', 'John')->push('items', 'boots');
DB::collection('users')->where('name', 'John')->push('items', array('sword', 'shield')); DB::collection('users')->where('name', 'John')->push('messages', array('from' => 'Jane Doe', 'message' => 'Hi John'));
**Pull** **Pull**
Remove one or more values from an array. Remove an item from an array.
DB::collection('users')->where('name', 'John')->pull('items', 'boots'); DB::collection('users')->where('name', 'John')->pull('items', 'boots');
DB::collection('users')->where('name', 'John')->pull('items', array('sword', 'shield')); DB::collection('users')->where('name', 'John')->pull('messages', array('from' => 'Jane Doe', 'message' => 'Hi John'));
**Unset** **Unset**
......
...@@ -458,11 +458,6 @@ class Builder extends \Illuminate\Database\Query\Builder { ...@@ -458,11 +458,6 @@ class Builder extends \Illuminate\Database\Query\Builder {
{ {
$query = array('$push' => $column); $query = array('$push' => $column);
} }
else if (is_array($value))
{
// $pushAll depricated
$query = array('$push' => array($column => array('$each' => $value)));
}
else else
{ {
$query = array('$push' => array($column => $value)); $query = array('$push' => array($column => $value));
...@@ -484,10 +479,6 @@ class Builder extends \Illuminate\Database\Query\Builder { ...@@ -484,10 +479,6 @@ class Builder extends \Illuminate\Database\Query\Builder {
{ {
$query = array('$pull' => $column); $query = array('$pull' => $column);
} }
else if (is_array($value))
{
$query = array('$pullAll' => array($column => $value));
}
else else
{ {
$query = array('$pull' => array($column => $value)); $query = array('$pull' => array($column => $value));
......
...@@ -196,7 +196,8 @@ class QueryBuilderTest extends PHPUnit_Framework_TestCase { ...@@ -196,7 +196,8 @@ class QueryBuilderTest extends PHPUnit_Framework_TestCase {
{ {
$id = DB::collection('users')->insertGetId(array( $id = DB::collection('users')->insertGetId(array(
'name' => 'John Doe', 'name' => 'John Doe',
'tags' => array() 'tags' => array(),
'messages' => array(),
)); ));
DB::collection('users')->where('_id', $id)->push('tags', 'tag1'); DB::collection('users')->where('_id', $id)->push('tags', 'tag1');
...@@ -213,19 +214,22 @@ class QueryBuilderTest extends PHPUnit_Framework_TestCase { ...@@ -213,19 +214,22 @@ class QueryBuilderTest extends PHPUnit_Framework_TestCase {
$this->assertEquals(2, count($user['tags'])); $this->assertEquals(2, count($user['tags']));
$this->assertEquals('tag2', $user['tags'][1]); $this->assertEquals('tag2', $user['tags'][1]);
DB::collection('users')->where('_id', $id)->push('tags', array('tag3', 'tag4')); $message = array('from' => 'Jane', 'body' => 'Hi John');
DB::collection('users')->where('_id', $id)->push('messages', $message);
$user = DB::collection('users')->find($id); $user = DB::collection('users')->find($id);
$this->assertTrue(is_array($user['tags'])); $this->assertTrue(is_array($user['messages']));
$this->assertEquals(4, count($user['tags'])); $this->assertEquals($message, $user['messages'][0]);
$this->assertEquals('tag4', $user['tags'][3]);
} }
public function testPull() public function testPull()
{ {
$message = array('from' => 'Jane', 'body' => 'Hi John');
$id = DB::collection('users')->insertGetId(array( $id = DB::collection('users')->insertGetId(array(
'name' => 'John Doe', 'name' => 'John Doe',
'tags' => array('tag1', 'tag2', 'tag3', 'tag4') 'tags' => array('tag1', 'tag2', 'tag3', 'tag4'),
'messages' => array($message)
)); ));
DB::collection('users')->where('_id', $id)->pull('tags', 'tag3'); DB::collection('users')->where('_id', $id)->pull('tags', 'tag3');
...@@ -235,12 +239,11 @@ class QueryBuilderTest extends PHPUnit_Framework_TestCase { ...@@ -235,12 +239,11 @@ class QueryBuilderTest extends PHPUnit_Framework_TestCase {
$this->assertEquals(3, count($user['tags'])); $this->assertEquals(3, count($user['tags']));
$this->assertEquals('tag4', $user['tags'][2]); $this->assertEquals('tag4', $user['tags'][2]);
DB::collection('users')->where('_id', $id)->pull('tags', array('tag2', 'tag4')); DB::collection('users')->where('_id', $id)->pull('messages', $message);
$user = DB::collection('users')->find($id); $user = DB::collection('users')->find($id);
$this->assertTrue(is_array($user['tags'])); $this->assertTrue(is_array($user['messages']));
$this->assertEquals(1, count($user['tags'])); $this->assertEquals(0, count($user['messages']));
$this->assertEquals('tag1', $user['tags'][0]);
} }
public function testDistinct() public function testDistinct()
......
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