PHPackages                             rasuvaeff/yii3-settings-db - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. rasuvaeff/yii3-settings-db

ActiveLibrary[Database &amp; ORM](/categories/database)

rasuvaeff/yii3-settings-db
==========================

Database-backed writable settings provider for Yii3 applications

v1.0.0(2d ago)00BSD-3-ClausePHPPHP ^8.3CI passing

Since Jun 7Pushed 2d agoCompare

[ Source](https://github.com/rasuvaeff/yii3-settings-db)[ Packagist](https://packagist.org/packages/rasuvaeff/yii3-settings-db)[ Docs](https://github.com/rasuvaeff/yii3-settings-db)[ RSS](/packages/rasuvaeff-yii3-settings-db/feed)WikiDiscussions master Synced 2d ago

READMEChangelogDependencies (13)Versions (2)Used By (0)

rasuvaeff/yii3-settings-db
==========================

[](#rasuvaeffyii3-settings-db)

[![Stable Version](https://camo.githubusercontent.com/2292d19cb275590dd27355177666e4caea7d7c009a9882740a81a3ddd64bb05d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7261737576616566662f796969332d73657474696e67732d64622e7376673f6c6162656c3d737461626c65)](https://packagist.org/packages/rasuvaeff/yii3-settings-db)[![Total Downloads](https://camo.githubusercontent.com/82f2805ee96d4423291bf8a29e1a392e5e78c9de2b8e47e3a4cb585b8510d9ef/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7261737576616566662f796969332d73657474696e67732d64622e737667)](https://packagist.org/packages/rasuvaeff/yii3-settings-db)[![Build](https://camo.githubusercontent.com/ac05bf8445501400f16e0a7e327e63706a803c1ebb19f0e46c5f15f8735e3cba/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f7261737576616566662f796969332d73657474696e67732d64622f6275696c642e796d6c3f6272616e63683d6d6173746572)](https://github.com/rasuvaeff/yii3-settings-db/actions)[![Static Analysis](https://camo.githubusercontent.com/729fb480791e438a7958144dd94f0e34d53234f83644aef6cce20e19bd2bc711/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f7261737576616566662f796969332d73657474696e67732d64622f7374617469632d616e616c797369732e796d6c3f6272616e63683d6d6173746572266c6162656c3d7073616c6d)](https://github.com/rasuvaeff/yii3-settings-db/actions)[![Coverage](https://camo.githubusercontent.com/3a07bd1794ca968ce91cd4eb151a5c46565b76757db9ac7f68b22a0c8e531b31/68747470733a2f2f636f6465636f762e696f2f67682f7261737576616566662f796969332d73657474696e67732d64622f6272616e63682f6d61737465722f67726170682f62616467652e737667)](https://codecov.io/gh/rasuvaeff/yii3-settings-db)[![PHP](https://camo.githubusercontent.com/847d9d563acfe4cf6969ad86bf493e134deecb86ecf27794eb1b30ba2262ea9a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646570656e64656e63792d762f7261737576616566662f796969332d73657474696e67732d64622f706870)](https://packagist.org/packages/rasuvaeff/yii3-settings-db)[![License](https://camo.githubusercontent.com/9f19cbe68639fd8c2e288d6f6dcb61e79dbeef7a57d22ac389fab7a5c66b41e6/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f7261737576616566662f796969332d73657474696e67732d64622e737667)](LICENSE.md)

Database-backed writable settings provider for Yii3 applications. Implements `WritableSettingsProvider` and `SettingsInspector` from `rasuvaeff/yii3-settings`, persists runtime overrides in a DB table, and supports at-rest encryption of secret settings via libsodium (XChaCha20-Poly1305).

> Using an AI coding assistant? [llms.txt](llms.txt) has a compact API reference you can feed into the model.

Requirements
------------

[](#requirements)

- PHP 8.3+
- `ext-sodium` (bundled with PHP 7.2+)
- `rasuvaeff/yii3-settings` ^1.1
- `yiisoft/db` ^2.0
- `yiisoft/db-migration` ^2.0
- a PSR-16 cache implementation — required transitively by `yiisoft/db` 2.0 (e.g. `yiisoft/cache`)

Installation
------------

[](#installation)

```
composer require rasuvaeff/yii3-settings-db
```

Usage
-----

[](#usage)

### Basic provider

[](#basic-provider)

```
use Rasuvaeff\Yii3Settings\SettingDefinition;
use Rasuvaeff\Yii3Settings\SettingType;
use Rasuvaeff\Yii3Settings\Settings;
use Rasuvaeff\Yii3SettingsDb\DbSettingsProvider;

$definitions = [
    'mail.from' => new SettingDefinition(key: 'mail.from', type: SettingType::String, default: 'noreply@example.com'),
    'orders.max_items' => new SettingDefinition(key: 'orders.max_items', type: SettingType::Int, default: 100),
];

$provider = new DbSettingsProvider(
    db: $connection,
    definitions: $definitions,
    table: 'settings',
);

$provider->set('mail.from', 'admin@example.com');
$provider->set('orders.max_items', '250');

$settings = new Settings(provider: $provider, definitions: $definitions);

$settings->string('mail.from');
$settings->int('orders.max_items');
```

The constructor accepts an optional `fallback` (`SettingsProvider`). When a key has no stored DB row, `get()` delegates to the fallback (which must recognize the same keys) instead of returning the definition default — this is how config `values` are preserved in the DI wiring below. Without a fallback, a missing row resolves to `SettingDefinition::default`.

### Yii3 config-plugin wiring

[](#yii3-config-plugin-wiring)

The package ships `config/params.php` and `config/di.php` via config-plugin. The default wiring keeps explicit config `values` working: `DbSettingsProvider`is built with a `ConfigSettingsProvider` fallback, so a key without a stored DB row resolves to its config `value` (and only then to the definition default).

```
return [
    'rasuvaeff/yii3-settings' => [
        'definitions' => [
            'mail.from' => ['type' => 'string', 'default' => 'noreply@example.com'],
            'orders.max_items' => ['type' => 'int', 'default' => 100],
        ],
        'values' => [
            'mail.from' => 'config@example.com',
        ],
    ],
    'rasuvaeff/yii3-settings-db' => [
        'table' => 'settings',
    ],
];
```

Runtime precedence after wiring:

SourcePriorityDB row1Explicit config `values`2`SettingDefinition::default`3### Migration

[](#migration)

Register the shipped migration path in your app config:

```
return [
    'yiisoft/db-migration' => [
        'sourcePaths' => [
            dirname(__DIR__) . '/vendor/rasuvaeff/yii3-settings-db/migrations',
        ],
    ],
];
```

Then run:

```
./yii migrate:up
./yii migrate:down --limit=1
```

### Core cache decorator

[](#core-cache-decorator)

```
use Rasuvaeff\Yii3Settings\CachedSettingsProvider;

$cached = new CachedSettingsProvider(
    inner: $provider,
    cache: $psr16Cache,
    definitions: $definitions,
    ttl: 60,
);

$cached->get('mail.from');
$cached->clear('mail.from');
```

### Secret settings

[](#secret-settings)

```
use Rasuvaeff\Yii3Settings\SettingDefinition;
use Rasuvaeff\Yii3Settings\SettingType;
use Rasuvaeff\Yii3SettingsDb\Crypto\KeyRing;
use Rasuvaeff\Yii3SettingsDb\Crypto\SodiumCipher;

$keyRing = new KeyRing(
    keys: ['key-2025' => sodium_crypto_aead_xchacha20poly1305_ietf_keygen()],
    activeKeyId: 'key-2025',
);
$cipher = new SodiumCipher(keyRing: $keyRing);

$definitions = [
    'billing.stripe_key' => new SettingDefinition(key: 'billing.stripe_key', type: SettingType::String, secret: true),
    'mail.from' => new SettingDefinition(key: 'mail.from', type: SettingType::String, default: 'noreply@example.com'),
];

$provider = new DbSettingsProvider(db: $db, definitions: $definitions, cipher: $cipher);

$provider->set('billing.stripe_key', 'sk_live_xxx');
// Stored as enc:vkey-2025:... in DB — plaintext never at rest.

$provider->get('billing.stripe_key'); // 'sk_live_xxx' (transparent decrypt)
```

### Settings inspector

[](#settings-inspector)

```
$state = $provider->describe(key: 'billing.stripe_key');
$state->key;               // 'billing.stripe_key'
$state->hasStoredOverride; // true
$state->source;            // 'db', 'config', or 'default'
$state->isSecret;          // true — value is masked (null)
$state->isWritable;        // true
```

### Key rotation

[](#key-rotation)

```
$count = $provider->reencryptSecrets();
// Decrypts all stored secret values with their current key,
// re-encrypts with the active key. Plaintext values (without enc: prefix)
// are encrypted in-place. Returns count of re-encrypted keys.
```

### Public API

[](#public-api)

ClassDescription`DbSettingsProvider`DB-backed `WritableSettingsProvider` + `SettingsInspector``KeyRing`Key management with versioning and active key`SodiumCipher`XChaCha20-Poly1305 encryption via libsodium`InvalidSettingRowException`Stored row type mismatchSecurity
--------

[](#security)

- Unknown keys are rejected: `get()`, `set()`, and `remove()` throw `UnknownSettingException`.
- DB values are normalized through `SettingDefinition`; malformed ints/floats/arrays throw `InvalidSettingRowException`.
- User values go through bound parameters in write/delete commands.
- Table names must be trusted application configuration.
- **At-rest encryption**: XChaCha20-Poly1305 AEAD via libsodium. Each value uses a random 24-byte nonce.
- **AAD binding**: ciphertext is bound to the setting key — a value cannot be moved to a different row.
- **Fail loud**: tampered data throws `DecryptionException`, never silent fallback.
- **Key rotation**: keyId in envelope allows reading with old key, writing with active key.
- **Secret values** do not appear in `SettingState::effectiveValue` (masked as null).
- Cipher is required when `secret: true` definitions exist — fail-fast at construction.

Examples
--------

[](#examples)

See [examples/](examples/) for runnable scripts.

Development
-----------

[](#development)

```
make install && make build
```

License
-------

[](#license)

BSD-3-Clause. See [LICENSE.md](LICENSE.md).

###  Health Score

40

—

FairBetter than 86% of packages

Maintenance99

Actively maintained with recent releases

Popularity0

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity48

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

2d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/b0812d5572a7041dfe36e222d295b2e6dc55833a605350fcde58a51a5965ed30?d=identicon)[rasuvaeff](/maintainers/rasuvaeff)

---

Top Contributors

[![rasuvaeff](https://avatars.githubusercontent.com/u/1352718?v=4)](https://github.com/rasuvaeff "rasuvaeff (3 commits)")

---

Tags

Settingsdatabaseproviderdbyii3

###  Code Quality

TestsPHPUnit

Static AnalysisPsalm, Rector

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/rasuvaeff-yii3-settings-db/health.svg)

```
[![Health](https://phpackages.com/badges/rasuvaeff-yii3-settings-db/health.svg)](https://phpackages.com/packages/rasuvaeff-yii3-settings-db)
```

###  Alternatives

[robmorgan/phinx

Phinx makes it ridiculously easy to manage the database migrations for your PHP app.

4.5k47.9M441](/packages/robmorgan-phinx)[spatie/laravel-translation-loader

Store your language lines in the database, yaml or other sources

8453.1M59](/packages/spatie-laravel-translation-loader)[aura/sqlquery

Object-oriented query builders for MySQL, Postgres, SQLite, and SQLServer; can be used with any database connection library.

4873.1M37](/packages/aura-sqlquery)[envms/fluentpdo

FluentPDO is a quick and light PHP library for rapid query building. It features a smart join builder, which automatically creates table joins.

923530.0k13](/packages/envms-fluentpdo)[arcanedev/laravel-settings

This package allows you to persists configs/settings for Laravel projects.

74136.5k6](/packages/arcanedev-laravel-settings)[danielme85/laravel-log-to-db

Custom Laravel Log channel handler that can store log events to SQL or MongoDB databases. Uses Laravel native logging functionality.

135979.2k1](/packages/danielme85-laravel-log-to-db)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
