CodeIgniter4
CodeIgniter4 copied to clipboard
What kind of object is the first param of setInsertBatch() / setUpdateBatch() ?
https://github.com/codeigniter4/CodeIgniter4/blob/fbc5afa2651cdd55860df1033da90a8a75411bf5/system/Database/BaseBuilder.php#L1639-L1641
https://github.com/codeigniter4/CodeIgniter4/blob/fbc5afa2651cdd55860df1033da90a8a75411bf5/system/Database/BaseBuilder.php#L2032-L2034
The the first param type is not set. So you can pass an object. But what kind of object?
The answer is like this:
$insertData = (object) [
'id' => [
1,
2,
],
'name' => [
'name1',
'name2',
],
'description' => [
'description1',
'description2',
],
];
That's how the current implementation works, but is that the intended specification?
--- a/tests/system/Database/Builder/InsertTest.php
+++ b/tests/system/Database/Builder/InsertTest.php
@@ -98,6 +98,39 @@ final class InsertTest extends CIUnitTestCase
$this->assertSame($expected, str_replace("\n", ' ', $query->getQuery()));
}
+ public function testSetInsertBatch()
+ {
+ $builder = $this->db->table('jobs');
+
+ $insertData = (object) [
+ 'id' => [
+ 1,
+ 2,
+ ],
+ 'name' => [
+ 'name1',
+ 'name2',
+ ],
+ 'description' => [
+ 'description1',
+ 'description2',
+ ],
+ ];
+
+ $this->db->shouldReturn('execute', 1)->shouldReturn('affectedRows', 1);
+ $builder->setInsertBatch($insertData);
+ $builder->insertBatch();
+
+ $query = $this->db->getLastQuery();
+ $this->assertInstanceOf(Query::class, $query);
+
+ $raw = 'INSERT INTO "jobs" ("description", "id", "name") VALUES (:description:,:id:,:name:), (:description.1:,:id.1:,:name.1:)';
+ $this->assertSame($raw, str_replace("\n", ' ', $query->getOriginalQuery()));
+
+ $expected = "INSERT INTO \"jobs\" (\"description\", \"id\", \"name\") VALUES ('description1',1,'name1'), ('description2',2,'name2')";
+ $this->assertSame($expected, str_replace("\n", ' ', $query->getQuery()));
+ }
+
/**
* @see https://github.com/codeigniter4/CodeIgniter4/issues/4345
*/
$ vendor/bin/phpunit tests/system/Database/Builder/InsertTest.php --no-coverage
PHPUnit 9.5.9 by Sebastian Bergmann and contributors.
Runtime: PHP 7.3.30
Configuration: /Users/kenji/work/codeigniter/CodeIgniter4/phpunit.xml.dist
....... 7 / 7 (100%)
Time: 00:00.130, Memory: 14.00 MB
OK (7 tests, 17 assertions)
Closed by #6536