Skip to content

Commit b8fe16d

Browse files
katie1348Katherinekenjis
authored
fix: BaseModel::insert() may not pass all the values from Entity (#4980)
* Update of BaseModel.php to fix INSERT issue Applied the fix that enables the INSERT to work correctly when inserting a row that already contains data. * Update BaseModel.php * Added tests for fix. User.php is an Entity UserObjModel.php returns an entity not StdObj * Updated to simplify usings. * Updated to cleanup test code. * Update tests/system/Models/InsertModelTest.php Co-authored-by: kenjis <[email protected]> * Update tests/_support/Entity/User.php Co-authored-by: kenjis <[email protected]> Co-authored-by: Katherine <[email protected]> Co-authored-by: kenjis <[email protected]>
1 parent d1515aa commit b8fe16d

File tree

4 files changed

+72
-1
lines changed

4 files changed

+72
-1
lines changed

system/BaseModel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1578,7 +1578,7 @@ protected function transformDataToArray($data, string $type): array
15781578
// properties representing the collection elements, we need to grab
15791579
// them as an array.
15801580
if (is_object($data) && ! $data instanceof stdClass) {
1581-
$data = $this->objectToArray($data, true, true);
1581+
$data = $this->objectToArray($data, ($type === 'update'), true);
15821582
}
15831583

15841584
// If it's still a stdClass, go ahead and convert to

tests/_support/Entity/User.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
/**
4+
* This file is part of CodeIgniter 4 framework.
5+
*
6+
* (c) CodeIgniter Foundation <[email protected]>
7+
*
8+
* For the full copyright and license information, please view
9+
* the LICENSE file that was distributed with this source code.
10+
*/
11+
12+
namespace Tests\Support\Entity;
13+
14+
use CodeIgniter\Entity\Entity;
15+
16+
class User extends Entity
17+
{
18+
protected $attributes = [
19+
'country' => 'India',
20+
];
21+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
/**
4+
* This file is part of CodeIgniter 4 framework.
5+
*
6+
* (c) CodeIgniter Foundation <[email protected]>
7+
*
8+
* For the full copyright and license information, please view
9+
* the LICENSE file that was distributed with this source code.
10+
*/
11+
12+
namespace Tests\Support\Models;
13+
14+
use CodeIgniter\Model;
15+
16+
class UserObjModel extends Model
17+
{
18+
protected $table = 'user';
19+
protected $allowedFields = [
20+
'name',
21+
'email',
22+
'country',
23+
'deleted_at',
24+
];
25+
protected $returnType = \Tests\Support\Entity\User::class;
26+
protected $useSoftDeletes = true;
27+
protected $dateFormat = 'datetime';
28+
}

tests/system/Models/InsertModelTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@
1515
use CodeIgniter\Entity\Entity;
1616
use CodeIgniter\I18n\Time;
1717
use stdClass;
18+
use Tests\Support\Entity\User;
1819
use Tests\Support\Models\JobModel;
1920
use Tests\Support\Models\UserModel;
21+
use Tests\Support\Models\UserObjModel;
2022
use Tests\Support\Models\WithoutAutoIncrementModel;
2123

2224
/**
@@ -286,4 +288,24 @@ public function testInsertWithSetAndEscape(): void
286288

287289
$this->assertCloseEnough(time(), strtotime($result->created_at));
288290
}
291+
292+
/**
293+
* @see https://github.com/codeigniter4/CodeIgniter4/issues/4247
294+
*/
295+
public function testInsertWithDefaultValue(): void
296+
{
297+
$this->createModel(UserObjModel::class);
298+
299+
$entity = new User();
300+
$entity->name = 'Mark';
301+
$entity->email = '[email protected]';
302+
$entity->country = 'India'; // same as the default
303+
$entity->deleted = 0;
304+
$entity->created_at = new Time('now');
305+
306+
$this->model->insert($entity);
307+
308+
$id = $this->model->getInsertID();
309+
$this->assertSame($entity->country, $this->model->find($id)->country);
310+
}
289311
}

0 commit comments

Comments
 (0)