Skip to content

Commit e1b64da

Browse files
authored
Merge pull request #2004 from MGatner/entity-haschanged-whole
Allow hasChanged() without parameter
2 parents 4699642 + 4abfe61 commit e1b64da

3 files changed

Lines changed: 21 additions & 2 deletions

File tree

system/Entity.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,13 +236,20 @@ public function syncOriginal()
236236

237237
/**
238238
* Checks a property to see if it has changed since the entity was created.
239+
* Or, without a parameter, checks if any properties have changed.
239240
*
240-
* @param string $key
241+
* @param ?string $key
241242
*
242243
* @return boolean
243244
*/
244-
public function hasChanged(string $key): bool
245+
public function hasChanged(string $key = null): bool
245246
{
247+
// If no parameter was given then check all attributes
248+
if ($key === null)
249+
{
250+
return $this->original !== $this->attributes;
251+
}
252+
246253
// Key doesn't exist in either
247254
if (! array_key_exists($key, $this->original) && ! array_key_exists($key, $this->attributes))
248255
{

tests/system/EntityTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,15 @@ public function testHasChangedNoChange()
664664
$this->assertFalse($entity->hasChanged('default'));
665665
}
666666

667+
public function testHasChangedWholeEntity()
668+
{
669+
$entity = $this->getEntity();
670+
671+
$entity->foo = 'bar';
672+
673+
$this->assertTrue($entity->hasChanged());
674+
}
675+
667676
protected function getEntity()
668677
{
669678
return new class extends Entity

user_guide_src/source/models/entities.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,3 +373,6 @@ attribute to check::
373373

374374
$user->name = 'Fred';
375375
$user->hasChanged('name'); // true
376+
377+
Or to check the whole entity for changed values omit the parameter:
378+
$user->hasChanged(); // true

0 commit comments

Comments
 (0)