From f42974a7a143e3adf6535a16e8ee8291466be8be Mon Sep 17 00:00:00 2001 From: Katherine Date: Thu, 29 Jul 2021 15:54:28 +0100 Subject: [PATCH 1/7] 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. --- system/BaseModel.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/BaseModel.php b/system/BaseModel.php index 1d777506976f..961506adebcd 100644 --- a/system/BaseModel.php +++ b/system/BaseModel.php @@ -1574,7 +1574,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 From c3d7e816e12490b8a022f3e8a9b1edf86d23480a Mon Sep 17 00:00:00 2001 From: Katherine Date: Fri, 30 Jul 2021 11:18:24 +0100 Subject: [PATCH 2/7] Update BaseModel.php --- system/BaseModel.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/BaseModel.php b/system/BaseModel.php index 961506adebcd..44b378e0c222 100644 --- a/system/BaseModel.php +++ b/system/BaseModel.php @@ -1574,7 +1574,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, ($type == 'update'), true); + $data = $this->objectToArray($data, ($type === 'update'), true); } // If it's still a stdClass, go ahead and convert to From dbd9348268248074855247fd3397b4b4be32c229 Mon Sep 17 00:00:00 2001 From: Katherine Date: Tue, 16 Nov 2021 09:28:33 +0000 Subject: [PATCH 3/7] Added tests for fix. User.php is an Entity UserObjModel.php returns an entity not StdObj --- tests/_support/Entity/User.php | 41 +++++++++++++++++++++++++ tests/_support/Models/UserObjModel.php | 31 +++++++++++++++++++ tests/system/Models/InsertModelTest.php | 25 +++++++++++++++ 3 files changed, 97 insertions(+) create mode 100644 tests/_support/Entity/User.php create mode 100644 tests/_support/Models/UserObjModel.php diff --git a/tests/_support/Entity/User.php b/tests/_support/Entity/User.php new file mode 100644 index 000000000000..6dbddd34c515 --- /dev/null +++ b/tests/_support/Entity/User.php @@ -0,0 +1,41 @@ + + * + * 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; + +class User extends Entity +{ + /** + * Maps names used in sets and gets against unique + * names within the class, allowing independence from + * database column names. + * + * Example: + * $datamap = [ + * 'db_name' => 'class_name' + * ]; + */ + protected $datamap = []; + + /** + * Define properties that are automatically converted to Time instances. + */ + // protected $dates = ['created_at', 'updated_at', 'deleted_at', 'expiry']; + protected $dates = ['created_at', 'expiry']; + + /** + * Array of field names and the type of value to cast them as + * when they are accessed. + */ + protected $casts = []; +} diff --git a/tests/_support/Models/UserObjModel.php b/tests/_support/Models/UserObjModel.php new file mode 100644 index 000000000000..383a79fc08ad --- /dev/null +++ b/tests/_support/Models/UserObjModel.php @@ -0,0 +1,31 @@ + + * + * 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'; + public $name = ''; + public $email = ''; + public $country = ''; +} diff --git a/tests/system/Models/InsertModelTest.php b/tests/system/Models/InsertModelTest.php index 521dec8fbadd..aebb3807db4c 100644 --- a/tests/system/Models/InsertModelTest.php +++ b/tests/system/Models/InsertModelTest.php @@ -286,4 +286,29 @@ public function testInsertWithSetAndEscape(): void $this->assertCloseEnough(time(), strtotime($result->created_at)); } + + public function testInsertWithUpdate(): void + { + $entity = new \Tests\Support\Entity\User(); + $this->createModel(UserObjModel::class); + + $entity->name = 'Mark'; + $entity->email = 'mark@example.com'; + $entity->country = 'India'; + $entity->deleted = 0; + $entity->created_at = new Time('now'); + + $this->model->insert($entity); + $this->assertGreaterThan(0, $this->model->getInsertID()); + $resultEntity = $this->model->where('name', 'Mark')->first(); + + $resultEntity->name = 'Katherine'; + + $this->model->insert($resultEntity); + $this->assertGreaterThan(0, $this->model->getInsertID()); + + $resultEntity2 = $this->model->where('name', 'Katherine')->first(); + $this->assertGreaterThan(0, $this->model->getInsertID()); + $this->assertSame('mark@example.com', $resultEntity2->email); + } } From e2f89810b4ec8383a7dc7e32a162fe660a9defca Mon Sep 17 00:00:00 2001 From: Katherine Date: Tue, 16 Nov 2021 09:45:19 +0000 Subject: [PATCH 4/7] Updated to simplify usings. --- tests/system/Models/InsertModelTest.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/system/Models/InsertModelTest.php b/tests/system/Models/InsertModelTest.php index aebb3807db4c..9175c980b56e 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; /** @@ -289,7 +291,7 @@ public function testInsertWithSetAndEscape(): void public function testInsertWithUpdate(): void { - $entity = new \Tests\Support\Entity\User(); + $entity = new User(); $this->createModel(UserObjModel::class); $entity->name = 'Mark'; From 4f51c43d5d0a95d95ce388897820f556749a7a81 Mon Sep 17 00:00:00 2001 From: Katherine Date: Wed, 17 Nov 2021 07:49:54 +0000 Subject: [PATCH 5/7] Updated to cleanup test code. --- tests/_support/Entity/User.php | 24 ++---------------------- tests/_support/Models/UserObjModel.php | 3 --- 2 files changed, 2 insertions(+), 25 deletions(-) diff --git a/tests/_support/Entity/User.php b/tests/_support/Entity/User.php index 6dbddd34c515..abeda417b026 100644 --- a/tests/_support/Entity/User.php +++ b/tests/_support/Entity/User.php @@ -15,27 +15,7 @@ class User extends Entity { - /** - * Maps names used in sets and gets against unique - * names within the class, allowing independence from - * database column names. - * - * Example: - * $datamap = [ - * 'db_name' => 'class_name' - * ]; - */ protected $datamap = []; - - /** - * Define properties that are automatically converted to Time instances. - */ - // protected $dates = ['created_at', 'updated_at', 'deleted_at', 'expiry']; - protected $dates = ['created_at', 'expiry']; - - /** - * Array of field names and the type of value to cast them as - * when they are accessed. - */ - protected $casts = []; + protected $dates = ['created_at', 'expiry']; + protected $casts = []; } diff --git a/tests/_support/Models/UserObjModel.php b/tests/_support/Models/UserObjModel.php index 383a79fc08ad..ea461ec64fb3 100644 --- a/tests/_support/Models/UserObjModel.php +++ b/tests/_support/Models/UserObjModel.php @@ -25,7 +25,4 @@ class UserObjModel extends Model protected $returnType = \Tests\Support\Entity\User::class; protected $useSoftDeletes = true; protected $dateFormat = 'datetime'; - public $name = ''; - public $email = ''; - public $country = ''; } From 2b3883781fdb9aa68a8d2c67a9751ae468a1c3ac Mon Sep 17 00:00:00 2001 From: Katherine Date: Fri, 19 Nov 2021 11:46:39 +0000 Subject: [PATCH 6/7] Update tests/system/Models/InsertModelTest.php Co-authored-by: kenjis --- tests/system/Models/InsertModelTest.php | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/tests/system/Models/InsertModelTest.php b/tests/system/Models/InsertModelTest.php index 9175c980b56e..abb246dba424 100644 --- a/tests/system/Models/InsertModelTest.php +++ b/tests/system/Models/InsertModelTest.php @@ -289,28 +289,23 @@ public function testInsertWithSetAndEscape(): void $this->assertCloseEnough(time(), strtotime($result->created_at)); } - public function testInsertWithUpdate(): void + /** + * @see https://github.com/codeigniter4/CodeIgniter4/issues/4247 + */ + public function testInsertWithDefaultValue(): void { - $entity = new User(); $this->createModel(UserObjModel::class); + $entity = new User(); $entity->name = 'Mark'; $entity->email = 'mark@example.com'; - $entity->country = 'India'; + $entity->country = 'India'; // same as the default $entity->deleted = 0; $entity->created_at = new Time('now'); $this->model->insert($entity); - $this->assertGreaterThan(0, $this->model->getInsertID()); - $resultEntity = $this->model->where('name', 'Mark')->first(); - $resultEntity->name = 'Katherine'; - - $this->model->insert($resultEntity); - $this->assertGreaterThan(0, $this->model->getInsertID()); - - $resultEntity2 = $this->model->where('name', 'Katherine')->first(); - $this->assertGreaterThan(0, $this->model->getInsertID()); - $this->assertSame('mark@example.com', $resultEntity2->email); + $id = $this->model->getInsertID(); + $this->assertSame($entity->country, $this->model->find($id)->country); } } From b1cd05a28156bc6cd24b5f82c10090c6645cc6c4 Mon Sep 17 00:00:00 2001 From: Katherine Date: Fri, 19 Nov 2021 11:46:47 +0000 Subject: [PATCH 7/7] Update tests/_support/Entity/User.php Co-authored-by: kenjis --- tests/_support/Entity/User.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/_support/Entity/User.php b/tests/_support/Entity/User.php index abeda417b026..dc8a3954341d 100644 --- a/tests/_support/Entity/User.php +++ b/tests/_support/Entity/User.php @@ -11,11 +11,11 @@ namespace Tests\Support\Entity; -use CodeIgniter\Entity; +use CodeIgniter\Entity\Entity; class User extends Entity { - protected $datamap = []; - protected $dates = ['created_at', 'expiry']; - protected $casts = []; + protected $attributes = [ + 'country' => 'India', + ]; }