Skip to content

Commit b4bc785

Browse files
committed
Merge remote-tracking branch 'origin/develop'
2 parents babb3ff + 98a8e4c commit b4bc785

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

app/Models/Component.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,18 @@ class Component extends SnipeModel
110110
'manufacturer' => ['name'],
111111
];
112112

113+
public static function booted()
114+
{
115+
static::saving(function ($model) {
116+
// We use 'sum_unconstrained_assets' as a 'cache' of the count of the sum of unconstrained assets, but
117+
// Eloquent will gladly try to save the value of that attribute in the case where we populate it ourselves.
118+
// But when it gets populated by 'withSum()' - it seems to work fine due to some Eloquent magic I am not
119+
// aware of. During a save, the quantity may have changed or other aspects may have changed, so
120+
// "invalidating the 'cache'" seems like a fair choice here.
121+
unset($model->sum_unconstrained_assets);
122+
});
123+
}
124+
113125

114126
public function isDeletable()
115127
{
@@ -255,6 +267,8 @@ public function numCheckedOut(bool $recalculate = false)
255267
if (is_null($this->sum_unconstrained_assets) || $recalculate) {
256268
// This, in a components-listing context, is mostly important for when it sets a 'zero' which
257269
// is *not* null - so we don't have to keep recalculating for un-checked-out components
270+
// NOTE: doing this will add a 'pseudo-attribute' to the component in question, so we need to _remove_ this
271+
// before we save - so that gets handled in the 'saving' callback defined in the 'booted' method, above.
258272
$this->sum_unconstrained_assets = $this->uncontrainedAssets()->sum('assigned_qty') ?? 0;
259273
}
260274
return $this->sum_unconstrained_assets;

0 commit comments

Comments
 (0)