Skip to content

Commit 19cc230

Browse files
authored
Added Hyperf\Database\Model\Model::discardChanges(). (#7261)
1 parent 102ca13 commit 19cc230

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

src/Model/Concerns/HasAttributes.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,16 @@ public function syncOriginalAttribute($attribute): static
521521
return $this->syncOriginalAttributes($attribute);
522522
}
523523

524+
/**
525+
* Discard attribute changes and reset the attributes to their original state.
526+
*/
527+
public function discardChanges(): static
528+
{
529+
[$this->attributes, $this->changes] = [$this->original, []];
530+
531+
return $this;
532+
}
533+
524534
/**
525535
* Sync multiple original attribute with their current values.
526536
*

tests/ModelTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,23 @@ public function testArrayAccessToAttributes()
206206
$this->assertFalse(isset($model['with']));
207207
}
208208

209+
public function testDiscardChanges()
210+
{
211+
$user = new ModelStub([
212+
'name' => 'Taylor Otwell',
213+
]);
214+
215+
$this->assertNotEmpty($user->isDirty());
216+
$this->assertNull($user->getOriginal('name'));
217+
$this->assertSame('Taylor Otwell', $user->getAttribute('name'));
218+
219+
$user->discardChanges();
220+
221+
$this->assertEmpty($user->isDirty());
222+
$this->assertNull($user->getOriginal('name'));
223+
$this->assertNull($user->getAttribute('name'));
224+
}
225+
209226
public function testOnly()
210227
{
211228
$model = new ModelStub();

0 commit comments

Comments
 (0)