Skip to content

Commit 272b11c

Browse files
committed
Implement hasChanges on Model and detect non changes to the value
1 parent b7959c1 commit 272b11c

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

src/AbstractModel.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
* You can obtain one at https://mozilla.org/MPL/2.0/.
1616
* @license MPL-2.0 https://mozilla.org/MPL/2.0/
1717
* @source https://github.com/wdes/simple-php-model-system
18+
* @version 1.1.0
1819
*/
1920

2021
abstract class AbstractModel
@@ -70,6 +71,14 @@ public function getValue(string $key)
7071
return $this->data[$key] ?? null;
7172
}
7273

74+
/**
75+
* @since 1.1.0
76+
*/
77+
public function hasChanges(): bool
78+
{
79+
return count($this->keysToModify) > 0;
80+
}
81+
7382
public function save(): bool
7483
{
7584
$keys = implode(', ', $this->getKeys());
@@ -412,6 +421,10 @@ public function setData(array $data): void
412421
*/
413422
protected function set($key, $value): void
414423
{
424+
if ($this->data[$key] === $value) {
425+
return;// No changes to apply
426+
}
427+
415428
$this->data[$key] = $value;
416429
$this->keysToModify[$key] = true;
417430
}

0 commit comments

Comments
 (0)