Skip to content
Closed
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/MySQLi/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion system/Database/OCI8/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion system/Database/Postgre/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion system/Database/SQLSRV/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion system/Database/SQLite3/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion system/Entity/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ public function hasChanged(?string $key = null): bool
*
* @return $this
*/
public function setAttributes(array $data)
public function injectRawData(array $data)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removing public function is BC, isn't it?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. I added beraking change label.

{
$this->attributes = $data;

Expand Down
30 changes: 30 additions & 0 deletions tests/system/Entity/EntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down