Repository navigation
Description
Problem
Config::postSaveLocations() (app/Controllers/Config.php) infers insert/update/delete/reorder/default intent from raw HTML form field names: stock_location[<id>] and new-<n> sort-order tokens. PHP assigns numeric keys to empty-bracket fields (stock_location[]), and those auto-assigned keys can collide with real numeric location_ids. The controller currently carries special-case collision-avoidance logic to work around this ambiguity.
Proposed fix
Raised by @coderabbitai during review of PR branch feature-stock-location-dropdown-order:
use a desired-state command instead of inferring intent from HTML field names.
The client should submit one ordered list. Each row should have a stable client key.
{ "locations": [ { "key": "id:3", "id": 3, "name": "Main", "isDefault": false }, { "key": "new:2d3d...", "id": null, "name": "Warehouse", "isDefault": true } ] }Then
postSaveLocations()can use this flow:
- Validate the complete request before changing the database.
- Load all active locations.
- Verify that every non-null id exists and is editable.
- Verify that names are valid and unique.
- Verify that exactly zero or one row has
isDefault.- Delete existing IDs that are absent from the submitted ID set.
- Update submitted rows with an existing ID.
- Insert submitted rows with a null ID.
- Build the sort order from the submitted list index.
- Resolve the default by its stable key, after inserts return their database IDs.
- Commit the transaction.
This makes each concern explicit:
- Delete: an existing database ID is absent from the command.
- Update: a submitted row has an existing database ID.
- Insert: a submitted row has no database ID.
- Order: list position.
- Default:
isDefault, or a separatedefaultKey.The important change is to avoid
stock_location[]andnew-<n>. PHP assigns numeric keys to empty-bracket fields. Those keys can collide with existing location IDs. A UUID client key avoids this ambiguity.A small command DTO would also reduce controller complexity:
final readonly class StockLocationCommand { public function __construct( public string $key, public ?int $locationId, public string $name, public bool $isDefault, public int $sortOrder ) { } }Move the reconciliation into a
Stock_locationservice or model method, for examplesaveConfiguration(array $commands): bool. The controller should only parse the request and return the response. The persistence method should own the transaction and the insert/update/delete/order/default invariants.
This would touch the controller, the Stock_location model, app/Views/partial/stock_locations.php, the stock-config JS (currently building stock_location[] fields and a comma-joined stock_location_order hidden input), and the existing controller tests.
Compatibility note
This should be designed compatibly with #4668 (proposal to refactor models to return CodeIgniter Entities instead of mixed StdClass/array, currently on hold). The saveConfiguration() method and StockLocationCommand DTO shape should avoid hard-coding assumptions that only work with raw arrays, so it isn't a blocker if/when that convention lands.
Activity
objecttothis commentedon Oct 22, 2026
Claude Issue Checker Agent (cc @objecttothis)
Investigated against current
master.Status: Still present (refactor not done).
Config::postSaveLocations()inapp/Controllers/Config.phpstill uses exactly the pattern described:No
StockLocationCommandDTO, no stable client keys, and nosaveConfiguration()method onStock_locationexist in the codebase — I found no matches forStockLocationCommandor similar desired-state naming anywhere in the repo. The insert/update/delete/order/default logic is still inferred fromstock_location[<id>]/new-<n>field-name conventions as originally described.