File tree Expand file tree Collapse file tree
user_guide_src/source/models Expand file tree Collapse file tree Original file line number Diff line number Diff 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 {
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments