From 29298011d6fc968e082cc557378ee974d534a65b Mon Sep 17 00:00:00 2001 From: kenjis Date: Thu, 28 Mar 2024 20:03:20 +0900 Subject: [PATCH 1/2] docs: fix incorrect doc comment --- system/Database/BaseBuilder.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/Database/BaseBuilder.php b/system/Database/BaseBuilder.php index 4163355b7ce3..8a33e220d84d 100644 --- a/system/Database/BaseBuilder.php +++ b/system/Database/BaseBuilder.php @@ -1526,7 +1526,7 @@ protected function _limit(string $sql, bool $offsetIgnore = false): string /** * Allows key/value pairs to be set for insert(), update() or replace(). * - * @param array|object|string $key Field name, or an array of field/value pairs + * @param array|object|string $key Field name, or an array of field/value pairs, or an object * @param mixed $value Field value, if $key is a single field * @param bool|null $escape Whether to escape values * From 3e34865139eee63f93d2d716ef08fd677ae6a126 Mon Sep 17 00:00:00 2001 From: kenjis Date: Thu, 28 Mar 2024 20:03:55 +0900 Subject: [PATCH 2/2] fix: Model::set() does not accept object --- system/Model.php | 7 +++- tests/system/Models/UpdateModelTest.php | 45 +++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/system/Model.php b/system/Model.php index 69f84c9e19c7..db912d7166d7 100644 --- a/system/Model.php +++ b/system/Model.php @@ -28,6 +28,7 @@ use ReflectionClass; use ReflectionException; use ReflectionProperty; +use stdClass; /** * The Model class extends BaseModel and provides additional @@ -674,7 +675,7 @@ public function builder(?string $table = null) * data here. This allows it to be used with any of the other * builder methods and still get validated data, like replace. * - * @param array|object|string $key Field name, or an array of field/value pairs + * @param array|object|string $key Field name, or an array of field/value pairs, or an object * @param bool|float|int|object|string|null $value Field value, if $key is a single field * @param bool|null $escape Whether to escape values * @@ -682,6 +683,10 @@ public function builder(?string $table = null) */ public function set($key, $value = '', ?bool $escape = null) { + if (is_object($key)) { + $key = $key instanceof stdClass ? (array) $key : $this->objectToArray($key); + } + $data = is_array($key) ? $key : [$key => $value]; foreach (array_keys($data) as $k) { diff --git a/tests/system/Models/UpdateModelTest.php b/tests/system/Models/UpdateModelTest.php index 4e30763a4073..d379256a6365 100644 --- a/tests/system/Models/UpdateModelTest.php +++ b/tests/system/Models/UpdateModelTest.php @@ -378,6 +378,51 @@ public function testUpdateWithEntityNoAllowedFields(): void $this->model->update($id, $entity); } + public function testUpdateSetObject(): void + { + $this->createModel(UserModel::class); + + $object = new stdClass(); + $object->name = 'Jones Martin'; + $object->email = 'jones@example.org'; + $object->country = 'India'; + + /** @var int|string $id */ + $id = $this->model->insert($object); + + /** @var stdClass $object */ + $object = $this->model->find($id); + $object->name = 'John Smith'; + + $return = $this->model->where('id', $id)->set($object)->update(); + + $this->assertTrue($return); + } + + public function testUpdateSetEntity(): void + { + $this->createModel(UserModel::class); + + $object = new stdClass(); + $object->id = 1; + $object->name = 'Jones Martin'; + $object->email = 'jones@example.org'; + $object->country = 'India'; + + $id = $this->model->insert($object); + + $entity = new Entity([ + 'id' => 1, + 'name' => 'John Smith', + 'email' => 'john@example.org', + 'country' => 'India', + ]); + + $return = $this->model->where('id', $id)->set($entity)->update(); + + $this->assertTrue($return); + } + public function testUpdateEntityWithPrimaryKeyCast(): void { if (