Skip to content

Commit

Permalink
fix: rename Entity::setAttributes() to avoid name collision
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjis committed Mar 4, 2022
1 parent fb4707e commit e583b34
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 6 deletions.
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)
{
$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

0 comments on commit e583b34

Please sign in to comment.