PHPackages                             ancor/yii2-related-kv-storage - 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. ancor/yii2-related-kv-storage

ActiveYii2-extension[Database &amp; ORM](/categories/database)

ancor/yii2-related-kv-storage
=============================

Related key-value storage in SQL Database.

01.6k4[1 issues](https://github.com/ancor-dev/yii2-related-kv-storage/issues)PHP

Since Sep 23Pushed 6y ago1 watchersCompare

[ Source](https://github.com/ancor-dev/yii2-related-kv-storage)[ Packagist](https://packagist.org/packages/ancor/yii2-related-kv-storage)[ RSS](/packages/ancor-yii2-related-kv-storage/feed)WikiDiscussions master Synced 4w ago

READMEChangelogDependenciesVersions (1)Used By (0)

Related key-value storage for Yii2.
===================================

[](#related-key-value-storage-for-yii2)

Description
-----------

[](#description)

**Storage bases on MySQL table. It can be use to edit settings from admin panel.**

**This extension has two classes:**

- [Config](#config-class) - storage for default settings
- [RelatedConfig](#relatedconfig-class) - storage for individual settings

Feel free to let me know what else you want added via:

- [Issues](https://github.com/ancor-dev/yii2-related-kv-storage/issues)

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

[](#installation)

The preferred way to install this extension is through [composer](http://getcomposer.org/download/).

Either run

```
$ php composer.phar require ancor/yii2-related-kv-storage
```

or add

```
"ancor/yii2-related-kv-storage": "dev-master"

```

to the `require` section of your `composer.json` file.

Config class
------------

[](#config-class)

**Simple key-value storage with array-like access.**

### Features:

[](#features)

- this class is the basis for RelatedConfig
- array access to options (also countable and iterable)
- this class can be use as yii component in $app container
- this class is designed for inheritance and expansion
- **autoload configuration** from database, when during instance creating

Storage bases on MySQL table. It can be use to edit settings from admin panel.

### Configuration

[](#configuration)

```
'components' => [
    'config' => [
        'class' => 'ancor\relatedKvStorage\Config',

        // default settings
        // 'tableName'  => '{{config}}',
        // 'keyField'   => 'key',
        // 'valueField' => 'value',
    ]
]
```

### Usage

[](#usage)

Simple usage

```
Yii::$app->config['main-page.show-banner'] = true;

if (Yii::$app->config['main-page.show-banner']) { ... }
```

Iterable

```
$config = Yii::$app->config;

foreach ($config as $key => $value) {
    echo $key . ' -> ' . $value . "\n";
}
```

Configurations was automatically loaded when instance created. But configurations will **not be save automatically.**

```
Yii::$app->config->attributes = [
    'default.option-one' => true,
    'default.option-two' => false,
];

// Can be get, but didn't store
echo Yii::$app->config['default.option-one']; // true

// For convenience, let's make some variable
$config = Yii::$app->config;

// Save to database
$config->save();

// let's change any value
$config['default.option-one'] = false;
echo $config['default.option-one']; // false

// And now, imagine that we need to reset changed values to default.
// Reload from database. Please note, ->save() was not fired.
$config->reload();
echo $config['default.option-one']; // true

// If we need to array type
echo gettype($config->attributes); // array
```

RelatedConfig class
-------------------

[](#relatedconfig-class)

**Simple key-value storage like [Config](#config-class), but related to other entity**

### Features:

[](#features-1)

- This class inherits all features from \[\[\\ancor\\relatedKvStorage\\Config\]\]
- Ideal for storing user preferences, and preferences need to has default values

**The value is extracted in three steps**

1. Try to get *current* value from this(RelatedConfig) model.
2. Try to get *default* value from common config component(Yii::$app-&gt;config).
3. If the value has been not received will be returned null.

*Please, create instance with help Yii::createObject() for configure instance.*

### Configuration

[](#configuration-1)

**It is best to use through model.** Example for class User

```
use ancor\relatedKvStorage\RelatedConfig

class User extends ... {

   ...

  /**
   * Get user configuration
   * @return RelatedConfig
   */
  public function getConfig() {
      $config = Yii::createObject([
          'class'      => RelatedConfig::className(),
          'relationId' => $this->id,

          // Default settings
          // 'tableName'           => '{{user_config}}',
          // 'relationIdField'     => 'user_id',
          // 'configComponentName' => 'config',
          // 'useCommonConfig'     => true,
      ]);
  }
}
```

### Usage

[](#usage-1)

```
// set default global settings (use Config class)
Yii::$app->config->attributes = [
   'user.dialogs.message-limit' => 100,
   'user.friends.limit => 20,
];

$user = new User();

$user->config = [

     // this option has not default value in global settings
    'user.dialogs.allow-modify' => true,

     // override default value from global settings
    'user.friends.limit' => 50,
];

// Must have! ( getConfig() reload don't cache `config` object and reload every time. So we didn't do it )
$user->config->save();

// use current value
echo $user->config['user.dialogs.allow-modify']; // true
// override default, use current value
echo $user->config['user.friends.limit']; // 50
// have not current, use default value
echo $user->config['user.dialogs.message-limit']; // 100
```

###  Health Score

23

—

LowBetter than 26% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity17

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity36

Early-stage or recently created project

 Bus Factor2

2 contributors hold 50%+ of commits

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.

### Community

Maintainers

![](https://www.gravatar.com/avatar/11b715dbbb813e6d3cd3c1bba6a6f7b8af7ffb0aa9d1b1a46a11910a2c41cb45?d=identicon)[ancor.dev](/maintainers/ancor.dev)

---

Top Contributors

[![Ancor-dev](https://avatars.githubusercontent.com/u/180199345?v=4)](https://github.com/Ancor-dev "Ancor-dev (1 commits)")[![jfkz](https://avatars.githubusercontent.com/u/328452?v=4)](https://github.com/jfkz "jfkz (1 commits)")[![korniychuk](https://avatars.githubusercontent.com/u/11693557?v=4)](https://github.com/korniychuk "korniychuk (1 commits)")

### Embed Badge

![Health badge](/badges/ancor-yii2-related-kv-storage/health.svg)

```
[![Health](https://phpackages.com/badges/ancor-yii2-related-kv-storage/health.svg)](https://phpackages.com/packages/ancor-yii2-related-kv-storage)
```

###  Alternatives

[jdorn/sql-formatter

a PHP SQL highlighting library

3.9k116.5M113](/packages/jdorn-sql-formatter)[propel/propel1

Propel is an open-source Object-Relational Mapping (ORM) for PHP5.

8351.6M87](/packages/propel-propel1)[jfelder/oracledb

Oracle DB driver for Laravel

11518.4k](/packages/jfelder-oracledb)

PHPackages © 2026

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