Repository: codeigniter4/CodeIgniter4 (PHP)

Issue:
Bug: Model::save($object) causes Error when `$useAutoIncrement` is false
### PHP Version

8.1

### CodeIgniter4 Version

4.2.11

### CodeIgniter4 Installation Method

Git

### Which operating systems have you tested for this bug?

macOS

### Which server did you use?

apache

### Database

n/a

### What happened?

```
Error : Cannot use object of type stdClass as array
 /.../CodeIgniter4/system/Model.php:688
```


### Steps to Reproduce

```diff
@@ -322,4 +322,27 @@ final class SaveModelTest extends LiveModelTestCase
         $this->assertSame($insert['key'], $this->model->getInsertID());
         $this->seeInDatabase('without_auto_increment', $update);
     }
+
+    public function testUseAutoIncrementSetToFalseSaveObject(): void
+    {
+        $this->createModel(WithoutAutoIncrementModel::class);
+
+        $insert = [
+            'key'   => 'some_random_key',
+            'value' => 'some value',
+        ];
+        $this->model->save((object) $insert);
+
+        $this->assertSame($insert['key'], $this->model->getInsertID());
+        $this->seeInDatabase('without_auto_increment', $insert);
+
+        $update = [
+            'key'   => 'some_random_key',
+            'value' => 'some different value',
+        ];
+        $this->model->save((object) $update);
+
+        $this->assertSame($insert['key'], $this->model->getInsertID());
+        $this->seeInDatabase('without_auto_increment', $update);
+    }
 }
```

### Expected Output

No error.

### Anything else?

Ref #6827


Discussion:
In the current implementation, an object is finally converted to an array in `BaseModel::insert()`.
https://github.com/codeigniter4/CodeIgniter4/blob/a2f09ca5f2407d126bdd5947e226c749b450c8fd/system/BaseModel.php#L710

Is there any issue if we convert in `Model`?




Patch:
diff --git a/system/Model.php b/system/Model.php
index 55b3b881de37..d91433914a1b 100644
--- a/system/Model.php
+++ b/system/Model.php
@@ -685,8 +685,12 @@ public function insert($data = null, bool $returnID = true)
             }
         }
 
-        if ($this->useAutoIncrement === false && isset($data[$this->primaryKey])) {
-            $this->tempPrimaryKeyValue = $data[$this->primaryKey];
+        if ($this->useAutoIncrement === false) {
+            if (is_array($data) && isset($data[$this->primaryKey])) {
+                $this->tempPrimaryKeyValue = $data[$this->primaryKey];
+            } elseif (is_object($data) && isset($data->{$this->primaryKey})) {
+                $this->tempPrimaryKeyValue = $data->{$this->primaryKey};
+            }
         }
 
         $this->escape   = $this->tempData['escape'] ?? [];


Test patch:
diff --git a/tests/system/Models/SaveModelTest.php b/tests/system/Models/SaveModelTest.php
index 287e96bec275..ad9311cec0e9 100644
--- a/tests/system/Models/SaveModelTest.php
+++ b/tests/system/Models/SaveModelTest.php
@@ -322,4 +322,27 @@ public function testUseAutoIncrementSetToFalseSave(): void
         $this->assertSame($insert['key'], $this->model->getInsertID());
         $this->seeInDatabase('without_auto_increment', $update);
     }
+
+    public function testUseAutoIncrementSetToFalseSaveObject(): void
+    {
+        $this->createModel(WithoutAutoIncrementModel::class);
+
+        $insert = [
+            'key'   => 'some_random_key',
+            'value' => 'some value',
+        ];
+        $this->model->save((object) $insert);
+
+        $this->assertSame($insert['key'], $this->model->getInsertID());
+        $this->seeInDatabase('without_auto_increment', $insert);
+
+        $update = [
+            'key'   => 'some_random_key',
+            'value' => 'some different value',
+        ];
+        $this->model->save((object) $update);
+
+        $this->assertSame($insert['key'], $this->model->getInsertID());
+        $this->seeInDatabase('without_auto_increment', $update);
+    }
 }