From b8fe16d2ff708971693a5e3325b74447ffb93fdd Mon Sep 17 00:00:00 2001 From: Katherine Date: Tue, 23 Nov 2021 11:49:00 +0000 Subject: [PATCH] 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 * Update tests/_support/Entity/User.php Co-authored-by: kenjis Co-authored-by: Katherine Co-authored-by: kenjis --- system/BaseModel.php | 2 +- tests/_support/Entity/User.php | 21 +++++++++++++++++++ tests/_support/Models/UserObjModel.php | 28 +++++++++++++++++++++++++ tests/system/Models/InsertModelTest.php | 22 +++++++++++++++++++ 4 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 tests/_support/Entity/User.php create mode 100644 tests/_support/Models/UserObjModel.php diff --git a/system/BaseModel.php b/system/BaseModel.php index 6b9ef3687817..971303664a8e 100644 --- a/system/BaseModel.php +++ b/system/BaseModel.php @@ -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 diff --git a/tests/_support/Entity/User.php b/tests/_support/Entity/User.php new file mode 100644 index 000000000000..dc8a3954341d --- /dev/null +++ b/tests/_support/Entity/User.php @@ -0,0 +1,21 @@ + + * + * 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', + ]; +} diff --git a/tests/_support/Models/UserObjModel.php b/tests/_support/Models/UserObjModel.php new file mode 100644 index 000000000000..ea461ec64fb3 --- /dev/null +++ b/tests/_support/Models/UserObjModel.php @@ -0,0 +1,28 @@ + + * + * 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'; +} diff --git a/tests/system/Models/InsertModelTest.php b/tests/system/Models/InsertModelTest.php index d08db6f87ac8..2bcc912e3d80 100644 --- a/tests/system/Models/InsertModelTest.php +++ b/tests/system/Models/InsertModelTest.php @@ -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; /** @@ -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 = 'mark@example.com'; + $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); + } }