PHPackages                             chemezov/yii2-dynamic-fields - 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. [Framework](/categories/framework)
4. /
5. chemezov/yii2-dynamic-fields

ActiveExtension[Framework](/categories/framework)

chemezov/yii2-dynamic-fields
============================

Behavior for Yii2 framework to work with custom (dynamic) fields

2.0(4y ago)212.3k↑22.2%MITPHP

Since Apr 29Pushed 4y ago1 watchersCompare

[ Source](https://github.com/chemezov/yii2-dynamic-fields)[ Packagist](https://packagist.org/packages/chemezov/yii2-dynamic-fields)[ RSS](/packages/chemezov-yii2-dynamic-fields/feed)WikiDiscussions master Synced 3w ago

READMEChangelog (4)Dependencies (1)Versions (5)Used By (0)

Yii2 Dynamic Fields behavoir
============================

[](#yii2-dynamic-fields-behavoir)

Behavior for Yii2 framework to work with custom (dynamic) fields.

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

[](#installation)

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

Either run

```
php composer.phar require --prefer-dist chemezov/yii2-dynamic-fields "*"

```

or add

```
"chemezov/yii2-dynamic-fields": "*"

```

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

Apply migrations:

```
yii migrate --migrationPath=@vendor/chemezov/yii2-dynamic-fields/migrations

```

Usage
-----

[](#usage)

```
use chemezov\yii2_dynamic_fields\DynamicFieldsBehavior;

class User extends \yii\db\ActiveRecord
{
    public function behaviors()
    {
        return [
            'dynamicFields' => [
                'class' => DynamicFieldsBehavior::class,
                'fields' => ['my_custom_string_1', 'my_custom_string_2'],
            ],
        ];
    }

    public function rules() {
        return [
            [['my_custom_string_1', 'my_custom_string_2'], 'string', 'max' => 255],
        ];
    }

    public function attributeLabels()
    {
        return [
            'my_custom_string_1' => 'My string 1',
            'my_custom_string_2' => 'My string 2',
        ];
    }
}
```

Now you can use your custom attributes:

```
$model = new User();
$value = $model->my_custom_string_1; // get value
$model->my_custom_string_1 = 'some value'; // set value
$model->save();
```

Other types
-----------

[](#other-types)

If you want to use other types than string you can use `AttributeTypecastBehavior`. Add `AttributeTypecastBehavior` after `DynamicFieldsBehavior`. **It is important!**

```
use chemezov\yii2_dynamic_fields\DynamicFieldsBehavior;
use yii\behaviors\AttributeTypecastBehavior;

class User extends \yii\db\ActiveRecord
{
    public function behaviors()
    {
        return [
            'dynamicFields' => [
                'class' => DynamicFieldsBehavior::class,
                'fields' => ['my_boolean_attribute'],
            ],
            'typecast' => [
                'class' => AttributeTypecastBehavior::class,
                'attributeTypes' => [
                    'my_boolean_attribute' => AttributeTypecastBehavior::TYPE_BOOLEAN,
                ],
                'typecastAfterFind' => true,
            ],
        ];
    }

    public function rules() {
        return [
            [['my_boolean_attribute'], 'default', 'value' => null],
            [['my_boolean_attribute'], 'boolean'],
        ];
    }

    public function attributeLabels()
    {
        return [
            'my_boolean_attribute' => 'My boolean attribute',
        ];
    }
}
```

Example
-------

[](#example)

For example you have User model. And you want to store **address** and some other values e.g. **is\_client** but don't want to extend your DB table. So you can use this behavior. Here example of User model:

```
use chemezov\yii2_dynamic_fields\DynamicFieldsBehavior;
use yii\behaviors\AttributeTypecastBehavior;

/**
 * Class User
 *
 * @property string $address
 * @property boolean $is_client
 */
class User extends \yii\db\ActiveRecord
{
    public function rules()
    {
        return [
        ...
            [$this->getAdditionalFieldsNamesString(), 'string', 'max' => 255],
            [$this->getAdditionalFieldsNamesBoolean(), 'default', 'value' => null],
            [$this->getAdditionalFieldsNamesBoolean(), 'boolean'],
        ];
    }

    public function behaviors()
    {
        return [
            'dynamicFields' => [
                'class' => DynamicFieldsBehavior::class,
                'fields' => $this->getAdditionalFieldsNames(),
            ],
            'typecast' => [
                'class' => AttributeTypecastBehavior::class,
                'attributeTypes' => [
                    'is_client' => AttributeTypecastBehavior::TYPE_BOOLEAN,
                ],
                'typecastAfterFind' => true,
            ],
        ];
    }

    public function getAdditionalFieldsNamesString()
    {
        return [
            'address',
        ];
    }

    public function getAdditionalFieldsNamesBoolean()
    {
        return [
            'is_client',
        ];
    }

    public function getAdditionalFieldsNames()
    {
        return array_merge($this->getAdditionalFieldsNamesString(), $this->getAdditionalFieldsNamesBoolean());
    }

    public function fields()
    {
        return array_merge(parent::fields(), $this->getAdditionalFieldsNames());
    }
}
```

Usage for JsonDynamicFieldsBehavior
-----------------------------------

[](#usage-for-jsondynamicfieldsbehavior)

Using this behavior is very similar to using it with the `DynamicFieldsBehavior`. This behavior store dynamic fields in separate column of model table in json.

You should create a column in your model table, e.g. `additional_data`, with type `text`, and allow `null` value. Example for migration:

```
$this->addColumn('your_table', 'additional_data', $this->text()->null());
```

```
use chemezov\yii2_dynamic_fields\JsonDynamicFieldsBehavior;
use yii\behaviors\AttributeTypecastBehavior;

class User extends \yii\db\ActiveRecord
{
    public function behaviors()
    {
        return [
            'dynamicFields' => [
                'class' => JsonDynamicFieldsBehavior::class,
                'fields' => ['my_boolean_attribute'],
            ],
            'typecast' => [
                'class' => AttributeTypecastBehavior::class,
                'attributeTypes' => [
                    'my_boolean_attribute' => AttributeTypecastBehavior::TYPE_BOOLEAN,
                ],
                'typecastAfterFind' => true,
            ],
        ];
    }

    public function rules() {
        return [
            [['my_boolean_attribute'], 'default', 'value' => null],
            [['my_boolean_attribute'], 'boolean'],
        ];
    }

    public function attributeLabels()
    {
        return [
            'my_boolean_attribute' => 'My boolean attribute',
        ];
    }
}
```

###  Health Score

33

—

LowBetter than 72% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity26

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity63

Established project with proven stability

 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

Every ~372 days

Total

4

Last Release

1498d ago

Major Versions

1.2 → 2.02022-05-20

### Community

Maintainers

![](https://www.gravatar.com/avatar/0e26168232be4ca7028de3fbdb0b7d1234c33b60f0683e01a69dfecd70adb0a2?d=identicon)[chemezov](/maintainers/chemezov)

---

Top Contributors

[![chemezov](https://avatars.githubusercontent.com/u/2368902?v=4)](https://github.com/chemezov "chemezov (7 commits)")

---

Tags

yii2extensionBehaviorcustom fieldsactiverecorddynamic-fieldseav

### Embed Badge

![Health badge](/badges/chemezov-yii2-dynamic-fields/health.svg)

```
[![Health](https://phpackages.com/badges/chemezov-yii2-dynamic-fields/health.svg)](https://phpackages.com/packages/chemezov-yii2-dynamic-fields)
```

###  Alternatives

[skeeks/cms

SkeekS CMS — control panel and tools based on php framework Yii2

13725.7k50](/packages/skeeks-cms)[mdmsoft/yii2-ar-behaviors

Behaviors for Yii2

1825.0k1](/packages/mdmsoft-yii2-ar-behaviors)

PHPackages © 2026

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