Skip to content

Commit

Permalink
Merge pull request #2004 from MGatner/entity-haschanged-whole
Browse files Browse the repository at this point in the history
Allow hasChanged() without parameter
  • Loading branch information
lonnieezell authored May 13, 2019
2 parents 4699642 + 4abfe61 commit e1b64da
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
11 changes: 9 additions & 2 deletions system/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,13 +236,20 @@ public function syncOriginal()

/**
* Checks a property to see if it has changed since the entity was created.
* Or, without a parameter, checks if any properties have changed.
*
* @param string $key
* @param ?string $key
*
* @return boolean
*/
public function hasChanged(string $key): bool
public function hasChanged(string $key = null): bool
{
// If no parameter was given then check all attributes
if ($key === null)
{
return $this->original !== $this->attributes;
}

// Key doesn't exist in either
if (! array_key_exists($key, $this->original) && ! array_key_exists($key, $this->attributes))
{
Expand Down
9 changes: 9 additions & 0 deletions tests/system/EntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,15 @@ public function testHasChangedNoChange()
$this->assertFalse($entity->hasChanged('default'));
}

public function testHasChangedWholeEntity()
{
$entity = $this->getEntity();

$entity->foo = 'bar';

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

protected function getEntity()
{
return new class extends Entity
Expand Down
3 changes: 3 additions & 0 deletions user_guide_src/source/models/entities.rst
Original file line number Diff line number Diff line change
Expand Up @@ -373,3 +373,6 @@ attribute to check::

$user->name = 'Fred';
$user->hasChanged('name'); // true

Or to check the whole entity for changed values omit the parameter:
$user->hasChanged(); // true

0 comments on commit e1b64da

Please sign in to comment.