Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Model::set() does not accept object #8670

Merged
merged 2 commits into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 = '[email protected]';
$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 = '[email protected]';
$object->country = 'India';

$id = $this->model->insert($object);

$entity = new Entity([
'id' => 1,
'name' => 'John Smith',
'email' => '[email protected]',
'country' => 'India',
]);

$return = $this->model->where('id', $id)->set($entity)->update();

$this->assertTrue($return);
}

public function testUpdateEntityWithPrimaryKeyCast(): void
{
if (
Expand Down
Loading