Skip to content

Commit 404e50b

Browse files
authored
Merge pull request #8670 from kenjis/fix-model-set-entity
fix: Model::set() does not accept object
2 parents 8e64913 + 3e34865 commit 404e50b

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed

system/Database/BaseBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1526,7 +1526,7 @@ protected function _limit(string $sql, bool $offsetIgnore = false): string
15261526
/**
15271527
* Allows key/value pairs to be set for insert(), update() or replace().
15281528
*
1529-
* @param array|object|string $key Field name, or an array of field/value pairs
1529+
* @param array|object|string $key Field name, or an array of field/value pairs, or an object
15301530
* @param mixed $value Field value, if $key is a single field
15311531
* @param bool|null $escape Whether to escape values
15321532
*

system/Model.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use ReflectionClass;
2929
use ReflectionException;
3030
use ReflectionProperty;
31+
use stdClass;
3132

3233
/**
3334
* The Model class extends BaseModel and provides additional
@@ -674,14 +675,18 @@ public function builder(?string $table = null)
674675
* data here. This allows it to be used with any of the other
675676
* builder methods and still get validated data, like replace.
676677
*
677-
* @param array|object|string $key Field name, or an array of field/value pairs
678+
* @param array|object|string $key Field name, or an array of field/value pairs, or an object
678679
* @param bool|float|int|object|string|null $value Field value, if $key is a single field
679680
* @param bool|null $escape Whether to escape values
680681
*
681682
* @return $this
682683
*/
683684
public function set($key, $value = '', ?bool $escape = null)
684685
{
686+
if (is_object($key)) {
687+
$key = $key instanceof stdClass ? (array) $key : $this->objectToArray($key);
688+
}
689+
685690
$data = is_array($key) ? $key : [$key => $value];
686691

687692
foreach (array_keys($data) as $k) {

tests/system/Models/UpdateModelTest.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,51 @@ public function testUpdateWithEntityNoAllowedFields(): void
378378
$this->model->update($id, $entity);
379379
}
380380

381+
public function testUpdateSetObject(): void
382+
{
383+
$this->createModel(UserModel::class);
384+
385+
$object = new stdClass();
386+
$object->name = 'Jones Martin';
387+
$object->email = '[email protected]';
388+
$object->country = 'India';
389+
390+
/** @var int|string $id */
391+
$id = $this->model->insert($object);
392+
393+
/** @var stdClass $object */
394+
$object = $this->model->find($id);
395+
$object->name = 'John Smith';
396+
397+
$return = $this->model->where('id', $id)->set($object)->update();
398+
399+
$this->assertTrue($return);
400+
}
401+
402+
public function testUpdateSetEntity(): void
403+
{
404+
$this->createModel(UserModel::class);
405+
406+
$object = new stdClass();
407+
$object->id = 1;
408+
$object->name = 'Jones Martin';
409+
$object->email = '[email protected]';
410+
$object->country = 'India';
411+
412+
$id = $this->model->insert($object);
413+
414+
$entity = new Entity([
415+
'id' => 1,
416+
'name' => 'John Smith',
417+
'email' => '[email protected]',
418+
'country' => 'India',
419+
]);
420+
421+
$return = $this->model->where('id', $id)->set($entity)->update();
422+
423+
$this->assertTrue($return);
424+
}
425+
381426
public function testUpdateEntityWithPrimaryKeyCast(): void
382427
{
383428
if (

0 commit comments

Comments
 (0)