Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add Entity::injectRawData() to avoid name collision #7208

Merged
merged 2 commits into from
Feb 13, 2023
Merged
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 @@ -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
2 changes: 2 additions & 0 deletions user_guide_src/source/changelogs/v4.4.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ Changes
Deprecations
************

- **Entity:** ``Entity::setAttributes()`` is deprecated. Use ``Entity::injectRawData()`` instead.

Bugs Fixed
**********

Expand Down