Skip to content

Commit 49a365e

Browse files
committed
model support
1 parent aa8ff46 commit 49a365e

File tree

1 file changed

+38
-2
lines changed

1 file changed

+38
-2
lines changed

src/Kitar/Dynamodb/Model/Model.php

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,40 @@ public function save(array $options = [])
171171
return $saved;
172172
}
173173

174+
/**
175+
* @inheritdoc
176+
*/
177+
protected function incrementOrDecrement($column, $amount, $extra, $method)
178+
{
179+
if (! $this->exists) {
180+
return $this->newQuery()
181+
->key($this->getKey())
182+
->{$method}($column, $amount, $extra);
183+
}
184+
185+
if ($this->fireModelEvent('updating') === false) {
186+
return false;
187+
}
188+
189+
return tap($this->newQuery()
190+
->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+
}
202+
203+
$this->setAttribute($column, $old);
204+
$this->fireModelEvent('updated', false);
205+
});
206+
}
207+
174208
/**
175209
* Perform a model update operation.
176210
*
@@ -326,10 +360,12 @@ public function __call($method, $parameters)
326360
"keyCondition",
327361
"keyConditionIn",
328362
"keyConditionBetween",
329-
'increment',
330-
'decrement'
331363
];
332364

365+
if (in_array($method, ['increment', 'decrement'])) {
366+
return $this->$method(...$parameters);
367+
}
368+
333369
if (! in_array($method, $allowedBuilderMethods)) {
334370
static::throwBadMethodCallException($method);
335371
}

0 commit comments

Comments
 (0)