Skip to content

Commit 8c753b4

Browse files
committed
sync model attributes
1 parent 06fa665 commit 8c753b4

File tree

2 files changed

+13
-16
lines changed

2 files changed

+13
-16
lines changed

src/Kitar/Dynamodb/Model/Model.php

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -188,19 +188,10 @@ protected function incrementOrDecrement($column, $amount, $extra, $method)
188188

189189
return tap($this->newQuery()
190190
->key($this->getKey())
191-
->{$method}($column, $amount, $extra), function () use($column, $amount, $method) {
192-
193-
$old = $this->{$column} ?? 0;
194-
switch ($method) {
195-
case 'increment':
196-
$old += $amount;
197-
break;
198-
case 'decrement':
199-
$old -= $amount;
200-
break;
201-
}
191+
->{$method}($column, $amount, $extra), function ($response) use($column, $amount, $method) {
192+
$this->forceFill($response['Attributes']);
193+
$this->syncChanges();
202194

203-
$this->setAttribute($column, $old);
204195
$this->fireModelEvent('updated', false);
205196
});
206197
}

tests/Query/BuilderTest.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -728,7 +728,8 @@ public function it_can_increment_value_of_attribute()
728728
':2' => [
729729
'S' => 'User A'
730730
]
731-
]
731+
],
732+
'ReturnValues' => 'UPDATED_NEW'
732733
];
733734

734735
$query = $this->newQuery('Thread')
@@ -739,9 +740,11 @@ public function it_can_increment_value_of_attribute()
739740
'LastPostedBy' => 'User A'
740741
]);
741742

743+
$processor = 'processSingleItem';
744+
742745
$this->assertEquals($method, $query['method']);
743746
$this->assertEquals($params, $query['params']);
744-
$this->assertNull($query['processor']);
747+
$this->assertEquals($processor, $query['processor']);
745748
}
746749

747750
/** @test */
@@ -770,9 +773,12 @@ public function it_can_decrement_value_of_attribute()
770773
':2' => [
771774
'S' => 'User A'
772775
]
773-
]
776+
],
777+
'ReturnValues' => 'UPDATED_NEW'
774778
];
775779

780+
$processor = 'processSingleItem';
781+
776782
$query = $this->newQuery('Thread')
777783
->key([
778784
'ForumName' => 'Laravel',
@@ -783,7 +789,7 @@ public function it_can_decrement_value_of_attribute()
783789

784790
$this->assertEquals($method, $query['method']);
785791
$this->assertEquals($params, $query['params']);
786-
$this->assertNull($query['processor']);
792+
$this->assertEquals($processor, $query['processor']);
787793
}
788794

789795
/** @test */

0 commit comments

Comments
 (0)