Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 905415a

Browse files
committed
Merge pull request laravel#7346 from konomae/patch-1
[5.0] Fix Multi-Row Insert For SQLite
2 parents 7de3c9d + 95155f7 commit 905415a

3 files changed

Lines changed: 28 additions & 2 deletions

File tree

src/Illuminate/Database/Query/Grammars/SQLiteGrammar.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public function compileInsert(Builder $query, array $values)
5656

5757
$columns = array_fill(0, count($values), implode(', ', $columns));
5858

59-
return "insert into $table ($names) select ".implode(' union select ', $columns);
59+
return "insert into $table ($names) select ".implode(' union all select ', $columns);
6060
}
6161

6262
/**

tests/Database/DatabaseEloquentIntegrationTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,32 @@ public function testBasicMorphManyRelationship()
242242
$this->assertEquals('First Post', $photos[3]->imageable->name);
243243
}
244244

245+
246+
public function testMultiInsertsWithDifferentValues()
247+
{
248+
$date = '1970-01-01';
249+
$result = EloquentTestPost::insert([
250+
['user_id' => 1, 'name' => 'Post', 'created_at' => $date, 'updated_at' => $date],
251+
['user_id' => 2, 'name' => 'Post', 'created_at' => $date, 'updated_at' => $date],
252+
]);
253+
254+
$this->assertTrue($result);
255+
$this->assertEquals(2, EloquentTestPost::count());
256+
}
257+
258+
259+
public function testMultiInsertsWithSameValues()
260+
{
261+
$date = '1970-01-01';
262+
$result = EloquentTestPost::insert([
263+
['user_id' => 1, 'name' => 'Post', 'created_at' => $date, 'updated_at' => $date],
264+
['user_id' => 1, 'name' => 'Post', 'created_at' => $date, 'updated_at' => $date],
265+
]);
266+
267+
$this->assertTrue($result);
268+
$this->assertEquals(2, EloquentTestPost::count());
269+
}
270+
245271
/**
246272
* Helpers...
247273
*/

tests/Database/DatabaseQueryBuilderTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -837,7 +837,7 @@ public function testInsertMethod()
837837
public function testSQLiteMultipleInserts()
838838
{
839839
$builder = $this->getSQLiteBuilder();
840-
$builder->getConnection()->shouldReceive('insert')->once()->with('insert into "users" ("email", "name") select ? as "email", ? as "name" union select ? as "email", ? as "name"', array('foo', 'taylor', 'bar', 'dayle'))->andReturn(true);
840+
$builder->getConnection()->shouldReceive('insert')->once()->with('insert into "users" ("email", "name") select ? as "email", ? as "name" union all select ? as "email", ? as "name"', array('foo', 'taylor', 'bar', 'dayle'))->andReturn(true);
841841
$result = $builder->from('users')->insert(array(array('email' => 'foo', 'name' => 'taylor'), array('email' => 'bar', 'name' => 'dayle')));
842842
$this->assertTrue($result);
843843
}

0 commit comments

Comments
 (0)