Skip to content

Commit

Permalink
feat: add Entity::injectRawData() to avoid name collision
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjis committed Feb 1, 2023
1 parent 0263a2d commit 31a2e83
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 7 deletions.
2 changes: 1 addition & 1 deletion system/Database/MySQLi/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,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')
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 @@ -112,7 +112,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 @@ -152,7 +152,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 @@ -140,7 +140,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
16 changes: 14 additions & 2 deletions system/Entity/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ public function hasChanged(?string $key = null): bool
*
* @return $this
*/
public function setAttributes(array $data)
public function injectRawData(array $data)
{
$this->attributes = $data;

Expand All @@ -288,6 +288,18 @@ public function setAttributes(array $data)
return $this;
}

/**
* Set raw data array without any mutations
*
* @return $this
*
* @deprecated Use injectRawData() instead.
*/
public function setAttributes(array $data)
{
return $this->injectRawData($data);
}

/**
* Checks the datamap to see if this property name is being mapped,
* and returns the db column name, if any, or the original property name.
Expand Down Expand Up @@ -449,7 +461,7 @@ public function __set(string $key, $value = null)
// so maybe wants to do sth with null value automatically
$method = 'set' . str_replace(' ', '', ucwords(str_replace(['-', '_'], ' ', $key)));

if (method_exists($this, $method)) {
if (method_exists($this, $method) && $method !== 'setAttributes') {
$this->{$method}($value);

return $this;
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 @@ -34,6 +34,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 31a2e83

Please sign in to comment.