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

fix: Entity can't handle column named attributes #5781

Closed
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
13 changes: 11 additions & 2 deletions system/Entity/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -439,12 +439,21 @@ public function __set(string $key, $value = null)

$value = $this->castAs($value, $key, 'set');

// if a set* method exists for this key, use that method to
// if a setter method exists for this key, use that method to
// insert this value. should be outside $isNullable check,
// so maybe wants to do sth with null value automatically
$method = 'set' . str_replace(' ', '', ucwords(str_replace(['-', '_'], ' ', $key)));

if (method_exists($this, $method)) {
// If a "`_set` + $key" method exists, it is a setter.
if (method_exists($this, '_' . $method)) {
$this->{$method}($value);

return $this;
}

// If a "`set` + $key" method exists, it is also a setter.
// But setAttributes() is not a setter for this.
if ($key !== 'attributes' && method_exists($this, $method)) {
Comment on lines +447 to +456
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// If a "`_set` + $key" method exists, it is a setter.
if (method_exists($this, '_' . $method)) {
$this->{$method}($value);
return $this;
}
// If a "`set` + $key" method exists, it is also a setter.
// But setAttributes() is not a setter for this.
if ($key !== 'attributes' && method_exists($this, $method)) {
// If a "`_set` + $key" method exists, it is a setter, or
// if a "`set` + $key" method exists, it is also a setter.
// But setAttributes() is not a setter for this.
if (method_exists($this, '_' . $method) || ($key !== 'attributes' && method_exists($this, $method))) {

Copy link
Contributor

Choose a reason for hiding this comment

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

Let's make code shorter,

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks, I found a bug in my code.

Why do you like shorter code?

$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 @@ -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