Skip to content

Commit 4d8a0e1

Browse files
committed
Added support for push, fixes mongodb#20
1 parent e0554e4 commit 4d8a0e1

File tree

3 files changed

+80
-10
lines changed

3 files changed

+80
-10
lines changed

Diff for: README.md

+9
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,15 @@ Or you can access the internal object directly:
243243

244244
User::raw()->find();
245245

246+
### MongoDB specific operations
247+
248+
***Push***
249+
250+
Add one or more items to an array.
251+
252+
User::where('name', 'John')->push('items', 'boots');
253+
User::where('name', 'John')->push('items', array('sword', 'shield'));
254+
246255
### Query Caching
247256

248257
You may easily cache the results of a query using the remember method:

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

+44-10
Original file line numberDiff line numberDiff line change
@@ -303,16 +303,7 @@ public function insertGetId(array $values, $sequence = null)
303303
*/
304304
public function update(array $values)
305305
{
306-
$update = array('$set' => $values);
307-
308-
$result = $this->collection->update($this->compileWheres(), $update, array('multiple' => true));
309-
310-
if (1 == (int) $result['ok'])
311-
{
312-
return $result['n'];
313-
}
314-
315-
return 0;
306+
return $this->performUpdate(array('$set' => $values));
316307
}
317308

318309
/**
@@ -420,6 +411,31 @@ public function raw($expression = null)
420411
return $this->collection;
421412
}
422413

414+
/**
415+
* Append a value to an array.
416+
*
417+
* @param string $column
418+
* @param mixed $value
419+
* @return int
420+
*/
421+
public function push($column, $value = null)
422+
{
423+
if (is_array($column))
424+
{
425+
$query = array('$push' => $column);
426+
}
427+
else if (is_array($value))
428+
{
429+
$query = array('$push' => array($column => array('$each' => $value)));
430+
}
431+
else
432+
{
433+
$query = array('$push' => array($column => $value));
434+
}
435+
436+
return $this->performUpdate($query);
437+
}
438+
423439
/**
424440
* Get a new instance of the query builder.
425441
*
@@ -430,6 +446,24 @@ public function newQuery()
430446
return new Builder($this->connection);
431447
}
432448

449+
/**
450+
* Perform update.
451+
*
452+
* @param array $query
453+
* @return int
454+
*/
455+
protected function performUpdate($query)
456+
{
457+
$result = $this->collection->update($this->compileWheres(), $query, array('multiple' => true));
458+
459+
if (1 == (int) $result['ok'])
460+
{
461+
return $result['n'];
462+
}
463+
464+
return 0;
465+
}
466+
433467
/**
434468
* Compile the where array
435469
*

Diff for: tests/QueryTest.php

+27
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,31 @@ public function testRaw()
101101
$this->assertInstanceOf('MongoCollection', $collection);
102102
}
103103

104+
public function testPush()
105+
{
106+
$user = array('name' => 'John Doe', 'tags' => array());
107+
$id = DB::collection('users')->insertGetId($user);
108+
109+
DB::collection('users')->where('_id', $id)->push('tags', 'tag1');
110+
$user = DB::collection('users')->find($id);
111+
112+
$this->assertTrue(is_array($user['tags']));
113+
$this->assertEquals(1, count($user['tags']));
114+
$this->assertEquals('tag1', $user['tags'][0]);
115+
116+
DB::collection('users')->where('_id', $id)->push('tags', 'tag2');
117+
$user = DB::collection('users')->find($id);
118+
119+
$this->assertTrue(is_array($user['tags']));
120+
$this->assertEquals(2, count($user['tags']));
121+
$this->assertEquals('tag2', $user['tags'][1]);
122+
123+
DB::collection('users')->where('_id', $id)->push('tags', array('tag3', 'tag4'));
124+
$user = DB::collection('users')->find($id);
125+
126+
$this->assertTrue(is_array($user['tags']));
127+
$this->assertEquals(4, count($user['tags']));
128+
$this->assertEquals('tag4', $user['tags'][3]);
129+
}
130+
104131
}

0 commit comments

Comments
 (0)