Skip to content

Commit d939e10

Browse files
authored
Merge pull request #504 from kenjis/fix-UserModel-cannot-save-array
fix: UserModel cannot save an array
2 parents 9d0b0c9 + ce1a0f7 commit d939e10

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

src/Models/UserModel.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,11 @@ public function update($id = null, $data = null): bool
253253
/** @throws DataException */
254254
$result = parent::update($id, $data);
255255
} catch (DataException $e) {
256+
// When $data is an array.
257+
if ($this->tempUser === null) {
258+
throw $e;
259+
}
260+
256261
$messages = [
257262
lang('Database.emptyDataset', ['update']),
258263
];

tests/Unit/UserModelTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Tests\Unit;
66

7+
use CodeIgniter\Database\Exceptions\DataException;
78
use CodeIgniter\Shield\Entities\User;
89
use CodeIgniter\Shield\Exceptions\LogicException;
910
use CodeIgniter\Shield\Models\UserModel;
@@ -239,4 +240,18 @@ public function testUpdateUserObjectWithoutUserDataToUpdate(): void
239240
'secret' => '[email protected]',
240241
]);
241242
}
243+
244+
/**
245+
* @see https://github.com/codeigniter4/shield/issues/471
246+
*/
247+
public function testSaveArrayNoDataToUpdate(): void
248+
{
249+
$this->expectException(DataException::class);
250+
$this->expectExceptionMessage('There is no data to update.');
251+
252+
$users = $this->createUserModel();
253+
$user = fake(UserModel::class);
254+
255+
$users->save(['id' => $user->id]);
256+
}
242257
}

0 commit comments

Comments
 (0)