A free open source IT asset/license management system
Normalize 0 IDs to null
snipe84555c4549a8
diff --git a/app/Http/Controllers/Assets/AssetsController.php b/app/Http/Controllers/Assets/AssetsController.phpindex 1c71ed83ae..d6accdd25e 100755--- a/app/Http/Controllers/Assets/AssetsController.php+++ b/app/Http/Controllers/Assets/AssetsController.php@@ -445,10 +445,14 @@ class AssetsController extends Controller$asset->requestable = $request->input('requestable', 0);$asset->rtd_location_id = $request->input('rtd_location_id', null);// Current location is editable from the asset edit form as of- // the location-dropdown addition. Blank clears it (the mutator- // normalizes '' / 0 to NULL) so the next checkout can re-derive- // location_id from the target. See general.location_edit_help.- $asset->location_id = $request->input('location_id', null);+ // the location-dropdown addition. Only overwrite when the key+ // is actually present in the request — a client that omits+ // location_id entirely (a partial update API caller, an older+ // form) still leaves the existing value intact. Present-but-+ // blank clears via the mutator (see setLocationIdAttribute).+ if ($request->has('location_id')) {+ $asset->location_id = $request->input('location_id');+ }$asset->byod = $request->input('byod', 0);$status = Statuslabel::find($request->input('status_id'));diff --git a/app/Models/Asset.php b/app/Models/Asset.phpindex 14b76c713d..5840a8e013 100644--- a/app/Models/Asset.php+++ b/app/Models/Asset.php@@ -113,33 +113,24 @@ class Asset extends Depreciable* assignment, never 0. Old data and previous bugs occasionally* left `0` behind (empty select2 → '' → integer-cast → 0), which* then breaks `exists:` validation and FMCS queries that treat- * NULL and 0 as different. Mutators normalize on write; matching- * accessors normalize on read so legacy rows already storing 0- * present as null at the model boundary until they're re-saved.- * Mirrors the parent_id pattern on Company / SnipeModel.+ * NULL and 0 as different. `set` normalizes on write, `get`+ * normalizes on read so legacy rows already storing 0 present as+ * null at the model boundary until they're re-saved.*/- public function setLocationIdAttribute($value): void+ protected function locationId(): Attribute{- $this->attributes['location_id'] = ($value === '' || $value === null || (int) $value === 0)- ? null- : (int) $value;- }-- public function getLocationIdAttribute($value): ?int- {- return ($value === null || (int) $value === 0) ? null : (int) $value;- }-- public function setCompanyIdAttribute($value): void- {- $this->attributes['company_id'] = ($value === '' || $value === null || (int) $value === 0)- ? null- : (int) $value;+ return Attribute::make(+ get: fn ($value) => ($value === null || (int) $value === 0) ? null : (int) $value,+ set: fn ($value) => ($value === '' || $value === null || (int) $value === 0) ? null : (int) $value,+ );}- public function getCompanyIdAttribute($value): ?int+ protected function companyId(): Attribute{- return ($value === null || (int) $value === 0) ? null : (int) $value;+ return Attribute::make(+ get: fn ($value) => ($value === null || (int) $value === 0) ? null : (int) $value,+ set: fn ($value) => ($value === '' || $value === null || (int) $value === 0) ? null : (int) $value,+ );}protected $rules = [