Skip to content

Commit 5de4168

Browse files
authored
Merge pull request #5672 from kenjis/fix-QB-insertBatch-ignore
fix: $builder->ignore()->insertBatch() only ignores on first iteration
2 parents 88c8c4a + cda21d4 commit 5de4168

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

system/Database/BaseBuilder.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1662,7 +1662,10 @@ public function insertBatch(?array $set = null, ?bool $escape = null, int $batch
16621662
}
16631663

16641664
if (! $hasQBSet) {
1665-
$this->resetWrite();
1665+
$this->resetRun([
1666+
'QBSet' => [],
1667+
'QBKeys' => [],
1668+
]);
16661669
}
16671670
}
16681671

system/Test/Mock/MockBuilder.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,9 @@
1515

1616
class MockBuilder extends BaseBuilder
1717
{
18+
protected $supportedIgnoreStatements = [
19+
'update' => 'IGNORE',
20+
'insert' => 'IGNORE',
21+
'delete' => 'IGNORE',
22+
];
1823
}

tests/system/Database/Builder/InsertTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,43 @@ public function testInsertBatch()
100100
$this->assertSame($expected, str_replace("\n", ' ', $query->getQuery()));
101101
}
102102

103+
/**
104+
* @see https://github.com/codeigniter4/CodeIgniter4/issues/5671
105+
*/
106+
public function testInsertBatchIgnore()
107+
{
108+
$builder = $this->db->table('jobs');
109+
110+
$insertData = [
111+
[
112+
'id' => 2,
113+
'name' => 'Commedian',
114+
'description' => 'There\'s something in your teeth',
115+
],
116+
[
117+
'id' => 3,
118+
'name' => 'Cab Driver',
119+
'description' => 'I am yellow',
120+
],
121+
];
122+
123+
$this->db->shouldReturn('execute', 1)->shouldReturn('affectedRows', 1);
124+
$builder->ignore()->insertBatch($insertData, true, 1);
125+
126+
$query = $this->db->getLastQuery();
127+
$this->assertInstanceOf(Query::class, $query);
128+
129+
$raw = <<<'SQL'
130+
INSERT IGNORE INTO "jobs" ("description", "id", "name") VALUES ('I am yellow',3,'Cab Driver')
131+
SQL;
132+
$this->assertSame($raw, str_replace("\n", ' ', $query->getOriginalQuery()));
133+
134+
$expected = <<<'SQL'
135+
INSERT IGNORE INTO "jobs" ("description", "id", "name") VALUES ('I am yellow',3,'Cab Driver')
136+
SQL;
137+
$this->assertSame($expected, str_replace("\n", ' ', $query->getQuery()));
138+
}
139+
103140
public function testInsertBatchWithoutEscape()
104141
{
105142
$builder = $this->db->table('jobs');

0 commit comments

Comments
 (0)