diff --git a/system/Entity/Entity.php b/system/Entity/Entity.php index 0cbcdef00904..a7058a678a16 100644 --- a/system/Entity/Entity.php +++ b/system/Entity/Entity.php @@ -308,7 +308,7 @@ public function hasChanged(?string $key = null): bool */ public function injectRawData(array $data) { - $this->attributes = $data; + $this->attributes = array_merge($this->attributes, $data); $this->syncOriginal(); diff --git a/tests/system/Entity/EntityTest.php b/tests/system/Entity/EntityTest.php index 8abc287956b4..8759b5b6a8a8 100644 --- a/tests/system/Entity/EntityTest.php +++ b/tests/system/Entity/EntityTest.php @@ -1296,6 +1296,52 @@ public function testJsonSerializableEntity(): void $this->assertSame(json_encode($entity->toArray()), json_encode($entity)); } + public function testInjectRawArray(): void + { + $entity = new class () extends Entity { + // The "user" property is not for DB + protected $attributes = [ + 'type' => 'Normal', + 'limit' => 10, + 'user' => 'John', + '_secure' => 'High', + ]; + protected $original = [ + 'type' => 'None', + 'limit' => 0, + 'user' => null, + '_secure' => 'Low', + ]; + }; + + $entity->injectRawData([ + 'type' => 'High', + 'limit' => 15, + '_secure' => 'Normal', + 'extra' => 'undefined', + ]); + + $this->assertSame( + [ + 'type' => 'High', + 'limit' => 15, + 'user' => 'John', + '_secure' => 'Normal', + 'extra' => 'undefined', + ], + $entity->toRawArray(), + ); + $this->assertSame( + [ + 'type' => 'High', + 'limit' => 15, + 'user' => 'John', + 'extra' => 'undefined', + ], + $entity->toArray(), + ); + } + private function getEntity(): object { return new class () extends Entity { diff --git a/user_guide_src/source/changelogs/v4.7.0.rst b/user_guide_src/source/changelogs/v4.7.0.rst index d91e9e2fd604..bebfc66e7cbf 100644 --- a/user_guide_src/source/changelogs/v4.7.0.rst +++ b/user_guide_src/source/changelogs/v4.7.0.rst @@ -147,6 +147,7 @@ Bugs Fixed ********** - **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. +- **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. See the repo's `CHANGELOG.md `_