Skip to content

Commit

Permalink
Fixed the wrong count of changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
limingxinleo committed Jan 7, 2021
1 parent 9b568dd commit bc5e02e
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 7 deletions.
6 changes: 4 additions & 2 deletions src/Model/Concerns/HasAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -594,9 +594,11 @@ public function syncOriginalAttributes($attributes)
*
* @return $this
*/
public function syncChanges()
public function syncChanges(array $columns = null)
{
$this->changes = $this->getDirty();
$changes = $this->getDirty();

$this->changes = is_array($columns) ? Arr::only($changes, $columns) : $changes;

return $this;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Model/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -1303,7 +1303,7 @@ protected function incrementOrDecrement($column, $amount, $extra, $method)

$columns = array_merge(array_keys($extra), [$column]);
return tap($this->setKeysForSaveQuery($query)->{$method}($column, $amount, $extra), function () use ($columns) {
$this->syncChanges();
$this->syncChanges($columns);

$this->syncOriginalAttributes($columns);
});
Expand Down
7 changes: 3 additions & 4 deletions tests/ModelRealBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,19 +146,18 @@ public function testIncrement()
$this->assertArrayHasKey('count', $ext->getChanges());
$this->assertSame(2, count($ext->getChanges()));

// TODO: Don't effect.
// Don't effect.
$ext->str = uniqid();
$this->assertTrue($ext->isDirty('str'));

$ext->increment('count', 1, [
'float_num' => (string) ($ext->float_num + 1),
]);
$this->assertTrue($ext->isDirty('str'));
$this->assertArrayHasKey('str', $ext->getChanges());
$this->assertArrayHasKey('count', $ext->getChanges());
$this->assertArrayHasKey('float_num', $ext->getChanges());

// TODO: It should be 2.
$this->assertSame(3, count($ext->getChanges()));
$this->assertSame(2, count($ext->getChanges()));
$this->assertTrue($ext->save());
$this->assertArrayHasKey('str', $ext->getChanges());
$this->assertSame(1, count($ext->getChanges()));
Expand Down

0 comments on commit bc5e02e

Please sign in to comment.