3
0
mirror of https://github.com/snipe/snipe-it.git synced 2026-02-05 01:15:51 +00:00

Unset the 'sum_unconstrained_assets' attribute before saving

This commit is contained in:
Brady Wetherington
2025-12-09 13:00:41 +00:00
parent 15c96f753c
commit bed6b04c3d

View File

@ -110,6 +110,18 @@ class Component extends SnipeModel
'manufacturer' => ['name'],
];
public static function booted()
{
static::saving(function ($model) {
// We use 'sum_unconstrained_assets' as a 'cache' of the count of the sum of unconstrained assets, but
// Eloquent will gladly try to save the value of that attribute in the case where we populate it ourselves.
// But when it gets populated by 'withSum()' - it seems to work fine due to some Eloquent magic I am not
// aware of. During a save, the quantity may have changed or other aspects may have changed, so
// "invalidating the 'cache'" seems like a fair choice here.
unset($model->sum_unconstrained_assets);
});
}
public function isDeletable()
{
@ -255,6 +267,8 @@ class Component extends SnipeModel
if (is_null($this->sum_unconstrained_assets) || $recalculate) {
// This, in a components-listing context, is mostly important for when it sets a 'zero' which
// is *not* null - so we don't have to keep recalculating for un-checked-out components
// NOTE: doing this will add a 'pseudo-attribute' to the component in question, so we need to _remove_ this
// before we save - so that gets handled in the 'saving' callback defined in the 'booted' method, above.
$this->sum_unconstrained_assets = $this->uncontrainedAssets()->sum('assigned_qty') ?? 0;
}
return $this->sum_unconstrained_assets;