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::hasChanged() is unreliable for casts #6284

Closed
wants to merge 3 commits into from
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
58 changes: 51 additions & 7 deletions system/Entity/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ public function toRawArray(bool $onlyChanged = false, bool $recursive = false):
}

foreach ($this->attributes as $key => $value) {
if (! $this->hasChanged($key)) {
if (! $this->hasChangedAttributes($key)) {
continue;
}

Expand Down Expand Up @@ -268,22 +268,45 @@ public function hasChanged(?string $key = null): bool
{
// If no parameter was given then check all attributes
if ($key === null) {
return $this->original !== $this->attributes;
return $this->hasChangedAttributes();
}

$dbColumn = $this->mapProperty($key);
$key = $this->mapProperty($key);

return $this->hasChangedAttributes($key);
}

/**
* Checks a attribute to see if it has changed since the entity
* was created. Or, without a parameter, checks if any
* attributes have changed.
*
* @param string|null $key key of $this->attributes
*/
private function hasChangedAttributes(?string $key = null): bool
kenjis marked this conversation as resolved.
Show resolved Hide resolved
{
// If no parameter was given then check all attributes
if ($key === null) {
foreach (array_keys($this->attributes) as $key) {
if ($this->isChanged($key)) {
return true;
}
}

return false;
}

// Key doesn't exist in either
if (! array_key_exists($dbColumn, $this->original) && ! array_key_exists($dbColumn, $this->attributes)) {
if (! array_key_exists($key, $this->original) && ! array_key_exists($key, $this->attributes)) {
return false;
}

// It's a new element
if (! array_key_exists($dbColumn, $this->original) && array_key_exists($dbColumn, $this->attributes)) {
if (! array_key_exists($key, $this->original) && array_key_exists($key, $this->attributes)) {
return true;
}

return $this->original[$dbColumn] !== $this->attributes[$dbColumn];
return $this->isChanged($key);
}

/**
Expand Down Expand Up @@ -526,7 +549,6 @@ public function __get(string $key)
// If a "`get` + $key" method exists, it is also a getter.
$result = $this->{$method}();
}

// Otherwise return the protected property
// if it exists.
elseif (array_key_exists($dbColumn, $this->attributes)) {
Expand All @@ -545,6 +567,28 @@ public function __get(string $key)
return $result;
}

/**
* Get cast value from the data array.
*
* @return array|bool|float|int|object|string|null
*/
private function _getCastData(string $key, array $data)
{
if (array_key_exists($key, $data)) {
return $this->castAs($data[$key], $key);
}

return null;
}

/**
* Check if the key value is changed.
*/
private function isChanged(string $key): bool
{
return $this->_getCastData($key, $this->original) !== $this->_getCastData($key, $this->attributes);
}

/**
* Returns true if a property exists names $key, or a getter method
* exists named like for __get().
Expand Down
45 changes: 45 additions & 0 deletions tests/system/Entity/EntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1032,6 +1032,51 @@ public function testHasChangedMappedChanged(): void
$this->assertTrue($entity->hasChanged('createdAt'));
}

/**
* @see https://github.com/codeigniter4/CodeIgniter4/issues/5905
*/
public function testHasChangedCastsItem()
{
$data = [
'id' => '1',
'name' => 'John',
'age' => '35',
];
$entity = new class ($data) extends \CodeIgniter\Entity {
protected $casts = [
'id' => 'integer',
'name' => 'string',
'age' => 'integer',
];
};
$entity->syncOriginal();

$entity->age = 35;

$this->assertFalse($entity->hasChanged('age'));
}

public function testHasChangedCastsWholeEntity()
{
$data = [
'id' => '1',
'name' => 'John',
'age' => '35',
];
$entity = new class ($data) extends \CodeIgniter\Entity {
protected $casts = [
'id' => 'integer',
'name' => 'string',
'age' => 'integer',
];
};
$entity->syncOriginal();

$entity->age = 35;

$this->assertFalse($entity->hasChanged());
}

public function testHasChangedWholeEntity(): void
{
$entity = $this->getEntity();
Expand Down
Loading