Skip to content

Commit da5e773

Browse files
author
Pantea Marius-ciclistu
committed
POC for laravel/framework#31778 sync trait
1 parent ab31b06 commit da5e773

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

src/Models/ExcessiveSetOptimizerOnSaveTrait.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,43 @@ trait ExcessiveSetOptimizerOnSaveTrait
2121
*/
2222
protected ?array $tmpOriginalBeforeAfterEvents = null;
2323

24+
/**
25+
* This will mass update the whole table if the model does not exist!
26+
* @inheritDoc
27+
* @throws \InvalidArgumentException
28+
*/
29+
protected function incrementOrDecrement($column, $amount, $extra, $method): int
30+
{
31+
if (!$this->exists) {
32+
return $this->newQueryWithoutRelationships()->{$method}($column, $amount, $extra);
33+
}
34+
35+
$this->{$column} = $this->isClassDeviable($column)
36+
? $this->deviateClassCastableAttribute($method, $column, $amount)
37+
: (\extension_loaded('bcmath') ? \bcadd(
38+
$s1 = (string)$this->{$column},
39+
$s2 = (string)($method === 'increment' ? $amount : $amount * -1),
40+
\max(\strlen(\strrchr($s1, '.') ?: ''), \strlen(\strrchr($s2, '.') ?: ''))
41+
) : $this->{$column} + ($method === 'increment' ? $amount : $amount * -1));
42+
43+
$this->forceFill($extra);
44+
45+
if (!$this->isDirty() || $this->fireModelEvent('updating') === false) {
46+
return 0;
47+
}
48+
49+
return (int)tap(
50+
$this->setKeysForSaveQuery($this->newQueryWithoutScopes())->{$method}($column, $amount, $extra),
51+
function () use ($column) {
52+
$this->syncChanges();
53+
54+
$this->fireModelEvent('updated', false);
55+
56+
$this->syncOriginalAttributes(\array_keys($this->changes));
57+
}
58+
);
59+
}
60+
2461
public function __call($method, $parameters)
2562
{
2663
$lowerMethod = \strtolower($method);

0 commit comments

Comments
 (0)