Skip to content

feat: add batch setting operations#161

Open
memleakd wants to merge 2 commits intocodeigniter4:developfrom
memleakd:feat/batch-setting-operations
Open

feat: add batch setting operations#161
memleakd wants to merge 2 commits intocodeigniter4:developfrom
memleakd:feat/batch-setting-operations

Conversation

@memleakd
Copy link
Copy Markdown

Description
Hello! This PR proposes adding explicit batch APIs for storing and forgetting multiple settings in one call:

  • Settings::setMany()
  • Settings::forgetMany()

This is offered as a small, generic addition for maintainers to review and adjust. The goal is to support applications and modules that already have a related group of settings to update, without requiring callers to issue separate set() or forget() calls for each item.

This can be useful for common cases like saving configuration forms, applying setup defaults, updating module preferences, importing settings, or resetting a group of related options. It also gives handlers a clear extension point for optimizing grouped writes while keeping existing handler behavior compatible.

Why

The limitations documentation notes that immediate writes can result in multiple database queries or file writes when several settings are changed in one request.

With this PR, callers can group those operations directly:

service('settings')->setMany([
    'App.siteName'  => 'My Great Site',
    'App.siteEmail' => 'support@example.com',
]);

service('settings')->forgetMany([
    'App.siteName',
    'App.siteEmail',
]);

This is also intended to complement the deferred writes work from #154. Deferred writes optimize multiple set() / forget() calls across the request lifecycle, while these methods provide an explicit API when the caller already has a batch of related changes.

Behavior

The new APIs follow the same behavior as repeated calls to set() and forget():

  • keys use the existing Class.property syntax
  • contexts work the same way
  • config fallback behavior is unchanged
  • duplicate keys resolve consistently, with the last value winning
  • no database schema changes are required

They also respect the current deferWrites setting:

  • when deferWrites is disabled, supported handlers persist the batch immediately
  • when deferWrites is enabled, batch calls are queued and persisted during post_system

Handler Support

BaseHandler includes default setMany() and forgetMany() implementations that call the existing set() and forget() methods for each item. Existing custom handlers keep working through this fallback.

Handlers can still override the batch methods when they can persist more efficiently:

  • DatabaseHandler groups batch inserts, updates, and deletes
  • FileHandler groups changes per class/context file

Tests and Docs

This PR adds tests for the public API, context-aware batch operations, duplicate key handling, database/file handler batch persistence, and deferred batch writes.

The user guide is updated with examples and clarifies how explicit batch APIs relate to deferred writes.

Checklist:

  • Securely signed commits
  • Component(s) with PHPDoc blocks, only if necessary or adds value
  • Unit testing, with >80% coverage
  • User guide updated
  • Conforms to style guide

Signed-off-by: memleakd <121398829+memleakd@users.noreply.github.com>
Signed-off-by: memleakd <121398829+memleakd@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant