Skip to content

Fields::namesToIds index never invalidated after save/delete/rename — fieldNameExists() lies in same request #2235

@adrianbj

Description

@adrianbj

Short description of the issue

The new namesToIds index and the methods that consume it (fieldNameExists(), getAllNames(), getAllIds(), getFieldId($name)) were added across commits d367e636, f029159e, 13a42239, 7e80f3a5, 789b5a05. The index is populated only once, in Fields::loadAllItems()___save(), ___delete(), and the rename branch never update it. Within the same request, those API methods return stale data for any field that was just created/renamed/deleted.

Expected behavior

Immediately after $fields->save($newField) in the same request:

  • $fields->fieldNameExists($newField->name) returns true.
  • $fields->getAllNames() and $fields->getAllIds() include the new field.
  • $fields->getFieldId($newField->name) returns the new ID.

Actual behavior

In wire/core/Fields/Fields.php:

  • namesToIds is populated only in loadAllItems() (line ~215).
  • ___save() does not update it.
  • ___delete() does not update it.
  • The rename branch ($item->prevName != $item->name, ~line 491) does not update it.
  • getFieldId($name) was changed from looking up via $this->get($field) to a direct lookup against namesToIds (line ~1711), so it now returns 0 for fields that were just created.

Optional: Suggestion for a possible fix

In ___save():

if($isNew) {
    $this->namesToIds[$item->name] = $item->id;
} else if($item->prevName && $item->prevName !== $item->name) {
    unset($this->namesToIds[$item->prevName]);
    $this->namesToIds[$item->name] = $item->id;
}

In ___delete():

unset($this->namesToIds[$item->name]);

Steps to reproduce the issue

$f = $fields->new('text', 'my_test_field');
$fields->save($f);
var_dump($fields->fieldNameExists('my_test_field')); // expected true, actual false
var_dump($fields->getFieldId('my_test_field')); // expected nonzero, actual 0

Setup/Environment

  • ProcessWire version: dev @ 15c749ed
  • File: wire/core/Fields/Fields.php
  • Introduced across commits d367e636, f029159e, 13a42239, 7e80f3a5, 789b5a05

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions