Skip to content

Commit 1e53349

Browse files
committed
Fixed push and pull behaviour for #41
1 parent 94b99a0 commit 1e53349

File tree

3 files changed

+19
-25
lines changed

3 files changed

+19
-25
lines changed

Diff for: README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -276,17 +276,17 @@ Or you can access the internal object directly:
276276

277277
**Push**
278278

279-
Add one or more items to an array.
279+
Add an items to an array.
280280

281281
DB::collection('users')->where('name', 'John')->push('items', 'boots');
282-
DB::collection('users')->where('name', 'John')->push('items', array('sword', 'shield'));
282+
DB::collection('users')->where('name', 'John')->push('messages', array('from' => 'Jane Doe', 'message' => 'Hi John'));
283283

284284
**Pull**
285285

286-
Remove one or more values from an array.
286+
Remove an item from an array.
287287

288288
DB::collection('users')->where('name', 'John')->pull('items', 'boots');
289-
DB::collection('users')->where('name', 'John')->pull('items', array('sword', 'shield'));
289+
DB::collection('users')->where('name', 'John')->pull('messages', array('from' => 'Jane Doe', 'message' => 'Hi John'));
290290

291291
**Unset**
292292

Diff for: src/Jenssegers/Mongodb/Builder.php

-9
Original file line numberDiff line numberDiff line change
@@ -458,11 +458,6 @@ public function push($column, $value = null)
458458
{
459459
$query = array('$push' => $column);
460460
}
461-
else if (is_array($value))
462-
{
463-
// $pushAll depricated
464-
$query = array('$push' => array($column => array('$each' => $value)));
465-
}
466461
else
467462
{
468463
$query = array('$push' => array($column => $value));
@@ -484,10 +479,6 @@ public function pull($column, $value = null)
484479
{
485480
$query = array('$pull' => $column);
486481
}
487-
else if (is_array($value))
488-
{
489-
$query = array('$pullAll' => array($column => $value));
490-
}
491482
else
492483
{
493484
$query = array('$pull' => array($column => $value));

Diff for: tests/QueryBuilderTest.php

+15-12
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,8 @@ public function testPush()
196196
{
197197
$id = DB::collection('users')->insertGetId(array(
198198
'name' => 'John Doe',
199-
'tags' => array()
199+
'tags' => array(),
200+
'messages' => array(),
200201
));
201202

202203
DB::collection('users')->where('_id', $id)->push('tags', 'tag1');
@@ -213,19 +214,22 @@ public function testPush()
213214
$this->assertEquals(2, count($user['tags']));
214215
$this->assertEquals('tag2', $user['tags'][1]);
215216

216-
DB::collection('users')->where('_id', $id)->push('tags', array('tag3', 'tag4'));
217-
217+
$message = array('from' => 'Jane', 'body' => 'Hi John');
218+
DB::collection('users')->where('_id', $id)->push('messages', $message);
219+
218220
$user = DB::collection('users')->find($id);
219-
$this->assertTrue(is_array($user['tags']));
220-
$this->assertEquals(4, count($user['tags']));
221-
$this->assertEquals('tag4', $user['tags'][3]);
221+
$this->assertTrue(is_array($user['messages']));
222+
$this->assertEquals($message, $user['messages'][0]);
222223
}
223224

224225
public function testPull()
225226
{
227+
$message = array('from' => 'Jane', 'body' => 'Hi John');
228+
226229
$id = DB::collection('users')->insertGetId(array(
227230
'name' => 'John Doe',
228-
'tags' => array('tag1', 'tag2', 'tag3', 'tag4')
231+
'tags' => array('tag1', 'tag2', 'tag3', 'tag4'),
232+
'messages' => array($message)
229233
));
230234

231235
DB::collection('users')->where('_id', $id)->pull('tags', 'tag3');
@@ -235,12 +239,11 @@ public function testPull()
235239
$this->assertEquals(3, count($user['tags']));
236240
$this->assertEquals('tag4', $user['tags'][2]);
237241

238-
DB::collection('users')->where('_id', $id)->pull('tags', array('tag2', 'tag4'));
239-
242+
DB::collection('users')->where('_id', $id)->pull('messages', $message);
243+
240244
$user = DB::collection('users')->find($id);
241-
$this->assertTrue(is_array($user['tags']));
242-
$this->assertEquals(1, count($user['tags']));
243-
$this->assertEquals('tag1', $user['tags'][0]);
245+
$this->assertTrue(is_array($user['messages']));
246+
$this->assertEquals(0, count($user['messages']));
244247
}
245248

246249
public function testDistinct()

0 commit comments

Comments
 (0)