Skip to content

Refactor: replace stock_location[]/new-<n> form hack with desired-state DTO command #4669

Open

Description

@objecttothis

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:

  1. Validate the complete request before changing the database.
  2. Load all active locations.
  3. Verify that every non-null id exists and is editable.
  4. Verify that names are valid and unique.
  5. Verify that exactly zero or one row has isDefault.
  6. Delete existing IDs that are absent from the submitted ID set.
  7. Update submitted rows with an existing ID.
  8. Insert submitted rows with a null ID.
  9. Build the sort order from the submitted list index.
  10. Resolve the default by its stable key, after inserts return their database IDs.
  11. 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 separate defaultKey.

The important change is to avoid stock_location[] and new-<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_location service or model method, for example saveConfiguration(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

  1. objecttothis commented on Oct 22, 2026

    @objecttothis
    MemberAuthor

    Claude Issue Checker Agent (cc @objecttothis)

    Investigated against current master.

    Status: Still present (refactor not done).

    Config::postSaveLocations() in app/Controllers/Config.php still uses exactly the pattern described:

    foreach ($this->request->getPost() as $key => $value) {
        if (str_contains($key, 'stock_location')) {
            foreach ($value as $location_id => $location_name) {
                ...
            }
        }
    }

    No StockLocationCommand DTO, no stable client keys, and no saveConfiguration() method on Stock_location exist in the codebase — I found no matches for StockLocationCommand or similar desired-state naming anywhere in the repo. The insert/update/delete/order/default logic is still inferred from stock_location[<id>]/new-<n> field-name conventions as originally described.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions