Skip to content

Commit 7222141

Browse files
committed
fix: Fixed Entity::injectRawData()
1 parent 01a45be commit 7222141

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

system/Entity/Entity.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ public function hasChanged(?string $key = null): bool
308308
*/
309309
public function injectRawData(array $data)
310310
{
311-
$this->attributes = $data;
311+
$this->attributes = array_merge($this->attributes, $data);
312312

313313
$this->syncOriginal();
314314

tests/system/Entity/EntityTest.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1296,6 +1296,52 @@ public function testJsonSerializableEntity(): void
12961296
$this->assertSame(json_encode($entity->toArray()), json_encode($entity));
12971297
}
12981298

1299+
public function testInjectRawArray(): void
1300+
{
1301+
$entity = new class () extends Entity {
1302+
// The "user" property is not for DB
1303+
protected $attributes = [
1304+
'type' => 'Normal',
1305+
'limit' => 10,
1306+
'user' => 'John',
1307+
'_secure' => 'High',
1308+
];
1309+
protected $original = [
1310+
'type' => 'None',
1311+
'limit' => 0,
1312+
'user' => null,
1313+
'_secure' => 'Low',
1314+
];
1315+
};
1316+
1317+
$entity->injectRawData([
1318+
'type' => 'High',
1319+
'limit' => 15,
1320+
'_secure' => 'Normal',
1321+
'extra' => 'undefined',
1322+
]);
1323+
1324+
$this->assertSame(
1325+
[
1326+
'type' => 'High',
1327+
'limit' => 15,
1328+
'user' => 'John',
1329+
'_secure' => 'Normal',
1330+
'extra' => 'undefined',
1331+
],
1332+
$entity->toRawArray(),
1333+
);
1334+
$this->assertSame(
1335+
[
1336+
'type' => 'High',
1337+
'limit' => 15,
1338+
'user' => 'John',
1339+
'extra' => 'undefined',
1340+
],
1341+
$entity->toArray(),
1342+
);
1343+
}
1344+
12991345
private function getEntity(): object
13001346
{
13011347
return new class () extends Entity {

user_guide_src/source/changelogs/v4.7.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ Bugs Fixed
147147
**********
148148

149149
- **Cookie:** The ``CookieInterface::SAMESITE_STRICT``, ``CookieInterface::SAMESITE_LAX``, and ``CookieInterface::SAMESITE_NONE`` constants are now written in ucfirst style to be consistent with usage in the rest of the framework.
150+
- **Entity:** Fixed a bug in ``Entity::injectRawArray()`` where the default values of ``$attributes`` values disappear. In the case when, after the DB query, the result did not contain all the values for the properties.
150151

151152
See the repo's
152153
`CHANGELOG.md <https://github.com/codeigniter4/CodeIgniter4/blob/develop/CHANGELOG.md>`_

0 commit comments

Comments
 (0)