Skip to content

Commit fb259e9

Browse files
committed
New batch insert detection, now detects batch inserts by looping all values and not only the first one
1 parent 06dacf1 commit fb259e9

File tree

2 files changed

+46
-10
lines changed

2 files changed

+46
-10
lines changed

src/Jenssegers/Mongodb/Builder.php

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,14 @@ public function getFresh($columns = array('*'))
106106

107107
foreach ($this->aggregate['columns'] as $column)
108108
{
109+
// Translate count into sum
109110
if ($function == 'count')
110111
{
111112
$group[$column] = array('$sum' => 1);
112113
}
113-
else {
114+
// Pass other functions directly
115+
else
116+
{
114117
$group[$column] = array('$' . $function => '$' . $column);
115118
}
116119
}
@@ -160,7 +163,7 @@ public function getFresh($columns = array('*'))
160163
}
161164

162165
/**
163-
* Generate the unique cache key for the query.
166+
* Generate the unique cache key for the current query.
164167
*
165168
* @return string
166169
*/
@@ -260,14 +263,21 @@ public function whereBetween($column, array $values, $boolean = 'and')
260263
*/
261264
public function insert(array $values)
262265
{
263-
// Since every insert gets treated like a batch insert, we will make sure the
264-
// bindings are structured in a way that is convenient for building these
265-
// inserts statements by verifying the elements are actually an array.
266-
if ( ! is_array(reset($values)))
266+
// Since every insert gets treated like a batch insert, we will have to detect
267+
// if the user is inserting a single document or an array of documents.
268+
$batch = true;
269+
foreach ($values as $value)
267270
{
268-
$values = array($values);
271+
// As soon as we find a value that is not an array we assume the user is
272+
// inserting a single document.
273+
if (!is_array($value))
274+
{
275+
$batch = false; break;
276+
}
269277
}
270278

279+
if (!$batch) $values = array($values);
280+
271281
// Batch insert
272282
return $this->collection->batchInsert($values);
273283
}
@@ -502,7 +512,7 @@ protected function compileWheres()
502512
// The new list of compiled wheres
503513
$wheres = array();
504514

505-
foreach ($this->wheres as $i=>&$where)
515+
foreach ($this->wheres as $i => &$where)
506516
{
507517
// Convert id's
508518
if (isset($where['column']) && $where['column'] == '_id')

tests/QueryTest.php

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,40 @@ public function testCollection()
2020

2121
public function testInsert()
2222
{
23-
$user = array('name' => 'John Doe');
23+
$user = array(
24+
'name' => 'John Doe',
25+
'tags' => array('tag1', 'tag2')
26+
);
2427
DB::collection('users')->insert($user);
2528

2629
$users = DB::collection('users')->get();
2730
$this->assertEquals(1, count($users));
2831

29-
$user = DB::collection('users')->first();
32+
$user = $users[0];
3033
$this->assertEquals('John Doe', $user['name']);
34+
$this->assertTrue(is_array($user['tags']));
35+
}
36+
37+
public function testBatchInsert()
38+
{
39+
$users = array(
40+
array(
41+
'name' => 'Jane Doe',
42+
'tags' => array('tag1', 'tag2')
43+
),
44+
array(
45+
'name' => 'John Doe',
46+
'tags' => array('tag3')
47+
),
48+
);
49+
DB::collection('users')->insert($users);
50+
51+
$users = DB::collection('users')->get();
52+
$this->assertEquals(2, count($users));
53+
54+
$user = $users[0];
55+
$this->assertEquals('Jane Doe', $user['name']);
56+
$this->assertTrue(is_array($user['tags']));
3157
}
3258

3359
public function testFind()

0 commit comments

Comments
 (0)