Skip to content

Commit

Permalink
Merge pull request #8670 from kenjis/fix-model-set-entity
Browse files Browse the repository at this point in the history
fix: Model::set() does not accept object
  • Loading branch information
kenjis authored Mar 29, 2024
2 parents 8e64913 + 3e34865 commit 404e50b
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
2 changes: 1 addition & 1 deletion system/Database/BaseBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
7 changes: 6 additions & 1 deletion system/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use ReflectionClass;
use ReflectionException;
use ReflectionProperty;
use stdClass;

/**
* The Model class extends BaseModel and provides additional
Expand Down Expand Up @@ -674,14 +675,18 @@ 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
*
* @return $this
*/
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) {
Expand Down
45 changes: 45 additions & 0 deletions tests/system/Models/UpdateModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down

0 comments on commit 404e50b

Please sign in to comment.