Skip to content

Commit

Permalink
Fixed push and pull behaviour for mongodb#41
Browse files Browse the repository at this point in the history
  • Loading branch information
mnphpexpert committed Sep 17, 2013
1 parent e0a5378 commit 63c6fa2
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 25 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -276,17 +276,17 @@ Or you can access the internal object directly:

**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', array('sword', 'shield'));
DB::collection('users')->where('name', 'John')->push('messages', array('from' => 'Jane Doe', 'message' => 'Hi John'));

**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', array('sword', 'shield'));
DB::collection('users')->where('name', 'John')->pull('messages', array('from' => 'Jane Doe', 'message' => 'Hi John'));

**Unset**

Expand Down
9 changes: 0 additions & 9 deletions src/Jenssegers/Mongodb/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -458,11 +458,6 @@ public function push($column, $value = null)
{
$query = array('$push' => $column);
}
else if (is_array($value))
{
// $pushAll depricated
$query = array('$push' => array($column => array('$each' => $value)));
}
else
{
$query = array('$push' => array($column => $value));
Expand All @@ -484,10 +479,6 @@ public function pull($column, $value = null)
{
$query = array('$pull' => $column);
}
else if (is_array($value))
{
$query = array('$pullAll' => array($column => $value));
}
else
{
$query = array('$pull' => array($column => $value));
Expand Down
27 changes: 15 additions & 12 deletions tests/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,8 @@ public function testPush()
{
$id = DB::collection('users')->insertGetId(array(
'name' => 'John Doe',
'tags' => array()
'tags' => array(),
'messages' => array(),
));

DB::collection('users')->where('_id', $id)->push('tags', 'tag1');
Expand All @@ -213,19 +214,22 @@ public function testPush()
$this->assertEquals(2, count($user['tags']));
$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);
$this->assertTrue(is_array($user['tags']));
$this->assertEquals(4, count($user['tags']));
$this->assertEquals('tag4', $user['tags'][3]);
$this->assertTrue(is_array($user['messages']));
$this->assertEquals($message, $user['messages'][0]);
}

public function testPull()
{
$message = array('from' => 'Jane', 'body' => 'Hi John');

$id = DB::collection('users')->insertGetId(array(
'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');
Expand All @@ -235,12 +239,11 @@ public function testPull()
$this->assertEquals(3, count($user['tags']));
$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);
$this->assertTrue(is_array($user['tags']));
$this->assertEquals(1, count($user['tags']));
$this->assertEquals('tag1', $user['tags'][0]);
$this->assertTrue(is_array($user['messages']));
$this->assertEquals(0, count($user['messages']));
}

public function testDistinct()
Expand Down

0 comments on commit 63c6fa2

Please sign in to comment.