Skip to content

Commit

Permalink
fix: BaseModel::insert() may not pass all the values from Entity (#4980)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
3 people authored Nov 23, 2021
1 parent d1515aa commit b8fe16d
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 1 deletion.
2 changes: 1 addition & 1 deletion system/BaseModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -1578,7 +1578,7 @@ protected function transformDataToArray($data, string $type): array
// properties representing the collection elements, we need to grab
// them as an array.
if (is_object($data) && ! $data instanceof stdClass) {
$data = $this->objectToArray($data, true, true);
$data = $this->objectToArray($data, ($type === 'update'), true);
}

// If it's still a stdClass, go ahead and convert to
Expand Down
21 changes: 21 additions & 0 deletions tests/_support/Entity/User.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

namespace Tests\Support\Entity;

use CodeIgniter\Entity\Entity;

class User extends Entity
{
protected $attributes = [
'country' => 'India',
];
}
28 changes: 28 additions & 0 deletions tests/_support/Models/UserObjModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

namespace Tests\Support\Models;

use CodeIgniter\Model;

class UserObjModel extends Model
{
protected $table = 'user';
protected $allowedFields = [
'name',
'email',
'country',
'deleted_at',
];
protected $returnType = \Tests\Support\Entity\User::class;
protected $useSoftDeletes = true;
protected $dateFormat = 'datetime';
}
22 changes: 22 additions & 0 deletions tests/system/Models/InsertModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
use CodeIgniter\Entity\Entity;
use CodeIgniter\I18n\Time;
use stdClass;
use Tests\Support\Entity\User;
use Tests\Support\Models\JobModel;
use Tests\Support\Models\UserModel;
use Tests\Support\Models\UserObjModel;
use Tests\Support\Models\WithoutAutoIncrementModel;

/**
Expand Down Expand Up @@ -286,4 +288,24 @@ public function testInsertWithSetAndEscape(): void

$this->assertCloseEnough(time(), strtotime($result->created_at));
}

/**
* @see https://github.com/codeigniter4/CodeIgniter4/issues/4247
*/
public function testInsertWithDefaultValue(): void
{
$this->createModel(UserObjModel::class);

$entity = new User();
$entity->name = 'Mark';
$entity->email = '[email protected]';
$entity->country = 'India'; // same as the default
$entity->deleted = 0;
$entity->created_at = new Time('now');

$this->model->insert($entity);

$id = $this->model->getInsertID();
$this->assertSame($entity->country, $this->model->find($id)->country);
}
}

0 comments on commit b8fe16d

Please sign in to comment.