Closed MaulanaMalikS closed 1 year ago
I've confirmed this bug.
CodeIgniter\Database\Exceptions\DataException : There is no primary key defined when trying to make insertBatch.
--- a/tests/system/Models/InsertModelTest.php
+++ b/tests/system/Models/InsertModelTest.php
@@ -65,6 +65,26 @@ final class InsertModelTest extends LiveModelTestCase
$this->seeInDatabase('job', ['name' => 'Cab Driver']);
}
+ public function testInsertBatchUseAutoIncrementSetToFalse(): void
+ {
+ $insertData = [
+ [
+ 'key' => 'key1',
+ 'value' => 'value1',
+ ],
+ [
+ 'key' => 'key2',
+ 'value' => 'value2',
+ ],
+ ];
+
+ $this->createModel(WithoutAutoIncrementModel::class);
+ $this->model->insertBatch($insertData);
+
+ $this->seeInDatabase('without_auto_increment', ['key' => 'key1']);
+ $this->seeInDatabase('without_auto_increment', ['key' => 'key2']);
+ }
+
public function testInsertBatchValidationFail(): void
{
$jobData = [
@MaulanaMalikS I sent #7759. Try it if you can.
@kenjis, yes, I've tried it and it works!
PHP Version
8.2
CodeIgniter4 Version
v4.3.3
CodeIgniter4 Installation Method
Composer (using
codeigniter4/appstarter
)Which operating systems have you tested for this bug?
Windows
Which server did you use?
cli-server (PHP built-in webserver)
Database
No response
What happened?
Model cannot insert using
insertBatch
when$useAutoIncrement
is set tofalse
.It's exactly the same as this issue: https://forum.codeigniter.com/showthread.php?tid=84733
But it occurs in
insertBatch
.Steps to Reproduce
Create a model, set
$useAutoIncrement
tofalse
, and exclude$primaryKey
from$allowedFields
as mentioned in the docs.Instantiate model,
$model = model(MyModel::class);
Utilize the
$model->insertBatch($data);
method to insert data, including the primary key.Expected Output
The data should be inserted into the database.
Anything else?
https://forum.codeigniter.com/showthread.php?tid=84733