diff --git a/system/Database/MySQLi/Result.php b/system/Database/MySQLi/Result.php index 7eab7d86c685..a40a1f2d1336 100644 --- a/system/Database/MySQLi/Result.php +++ b/system/Database/MySQLi/Result.php @@ -142,7 +142,7 @@ protected function fetchAssoc() protected function fetchObject(string $className = 'stdClass') { if (is_subclass_of($className, Entity::class)) { - return empty($data = $this->fetchAssoc()) ? false : (new $className())->setAttributes($data); + return empty($data = $this->fetchAssoc()) ? false : (new $className())->injectRawData($data); } return $this->resultID->fetch_object($className); diff --git a/system/Database/OCI8/Result.php b/system/Database/OCI8/Result.php index ce3a73def1c4..60ee1d7770c8 100644 --- a/system/Database/OCI8/Result.php +++ b/system/Database/OCI8/Result.php @@ -102,7 +102,7 @@ protected function fetchObject(string $className = stdClass::class) return $row; } if (is_subclass_of($className, Entity::class)) { - return (new $className())->setAttributes((array) $row); + return (new $className())->injectRawData((array) $row); } $instance = new $className(); diff --git a/system/Database/Postgre/Result.php b/system/Database/Postgre/Result.php index 76a0dd956515..d888e0b97d11 100644 --- a/system/Database/Postgre/Result.php +++ b/system/Database/Postgre/Result.php @@ -108,7 +108,7 @@ protected function fetchAssoc() protected function fetchObject(string $className = 'stdClass') { if (is_subclass_of($className, Entity::class)) { - return empty($data = $this->fetchAssoc()) ? false : (new $className())->setAttributes($data); + return empty($data = $this->fetchAssoc()) ? false : (new $className())->injectRawData($data); } return pg_fetch_object($this->resultID, null, $className); diff --git a/system/Database/SQLSRV/Result.php b/system/Database/SQLSRV/Result.php index 2a55e452ac09..4884055b9a02 100755 --- a/system/Database/SQLSRV/Result.php +++ b/system/Database/SQLSRV/Result.php @@ -150,7 +150,7 @@ protected function fetchAssoc() protected function fetchObject(string $className = 'stdClass') { if (is_subclass_of($className, Entity::class)) { - return empty($data = $this->fetchAssoc()) ? false : (new $className())->setAttributes($data); + return empty($data = $this->fetchAssoc()) ? false : (new $className())->injectRawData($data); } return sqlsrv_fetch_object($this->resultID, $className); diff --git a/system/Database/SQLite3/Result.php b/system/Database/SQLite3/Result.php index 6afc04c51578..d660b793fba9 100644 --- a/system/Database/SQLite3/Result.php +++ b/system/Database/SQLite3/Result.php @@ -136,7 +136,7 @@ protected function fetchObject(string $className = 'stdClass') $classObj = new $className(); if (is_subclass_of($className, Entity::class)) { - return $classObj->setAttributes($row); + return $classObj->injectRawData($row); } $classSet = Closure::bind(function ($key, $value) { diff --git a/system/Entity/Entity.php b/system/Entity/Entity.php index 194f7441717b..91cd96714834 100644 --- a/system/Entity/Entity.php +++ b/system/Entity/Entity.php @@ -274,7 +274,7 @@ public function hasChanged(?string $key = null): bool * * @return $this */ - public function setAttributes(array $data) + public function injectRawData(array $data) { $this->attributes = $data; diff --git a/tests/system/Entity/EntityTest.php b/tests/system/Entity/EntityTest.php index a9a59502575f..354851ffb288 100644 --- a/tests/system/Entity/EntityTest.php +++ b/tests/system/Entity/EntityTest.php @@ -32,6 +32,36 @@ final class EntityTest extends CIUnitTestCase { use ReflectionHelper; + public function testSetStringToPropertyNamedAttributes() + { + $entity = $this->getEntity(); + + $entity->attributes = 'attributes'; + + $this->assertSame('attributes', $entity->attributes); + } + + /** + * @see https://github.com/codeigniter4/CodeIgniter4/issues + */ + public function testSetArrayToPropertyNamedAttributes() + { + $entity = new Entity(); + + $entity->a = 1; + $entity->attributes = [1, 2, 3]; + + $expected = [ + 'a' => 1, + 'attributes' => [ + 0 => 1, + 1 => 2, + 2 => 3, + ], + ]; + $this->assertSame($expected, $entity->toRawArray()); + } + public function testSimpleSetAndGet() { $entity = $this->getEntity();