PHPackages                             my6uot9/yii2-dynamic-ar - 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. my6uot9/yii2-dynamic-ar

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

my6uot9/yii2-dynamic-ar
=======================

Extends Yii ActiveRecord for Maria Dynamic Columns

0.4.3(5y ago)03[1 PRs](https://github.com/My6UoT9/yii2-dynamic-ar/pulls)ISCPHP

Since May 21Pushed 4y agoCompare

[ Source](https://github.com/My6UoT9/yii2-dynamic-ar)[ Packagist](https://packagist.org/packages/my6uot9/yii2-dynamic-ar)[ RSS](/packages/my6uot9-yii2-dynamic-ar/feed)WikiDiscussions master Synced 2d ago

READMEChangelogDependencies (5)Versions (18)Used By (0)

Dynamic Active Record
=====================

[](#dynamic-active-record)

This is a fork of [Tom Worster's version](https://github.com/tom--/yii2-dynamic-ar).

The [yii2-dynamic-ar](https://github.com/My6UoT9/yii2-dynamic-ar) extension adds NoSQL-like documents to [Yii 2 Framework](http://www.yiiframework.com/)'s [Active Record ORM](http://www.yiiframework.com/doc-2.0/guide-db-active-record.html).

### Maria Dynamic Columns and PostgreSQL jsonb

[](#maria-dynamic-columns-and-postgresql-jsonb)

[Dynamic Columns](https://mariadb.com/kb/en/mariadb/dynamic-columns/)in [Maria 10.0](https://mariadb.com/kb/en/mariadb/what-is-mariadb-100/)+ and [jsonb column types](http://www.postgresql.org/docs/9.4/static/datatype-json.html)and [functions](http://www.postgresql.org/docs/9.4/static/functions-json.html) in in [PostgreSQL 9.4](http://www.postgresql.org/)+ provide, in effect, a [NoSQL document](https://en.wikipedia.org/wiki/Document-oriented_database)attached to every row of an SQL table. It's a powerful feature that allows you to do things that have been hard in relational DBs. Problems that might drive you to Couch or Mongo, or to commit a crime like [EAV](https://en.wikipedia.org/wiki/Entity%E2%80%93attribute%E2%80%93value_model)to your schema, can suddenly be easy when

- records can have any number of attributes,
- attribute names can be made up on the fly,
- the dynamic attribute names don't appear in the schema,
- dynamic attributes can be structured like an associative array.

Dynamic AR works for Maria now and will come to PostgreSQL in the future.

Example
-------

[](#example)

An online shopping site has a table that stores info about each product.

```
CREATE TABLE product (
    id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
    sku VARCHAR(32),
    upc VARCHAR(32),
    title VARCHAR(255),
    price DECIMAL(9, 2),
    stock INT(11),
    details LONGBLOB NOT NULL
);
```

In this (simplistic) example, `details` will hold the Maria [Dynamic Column blob](https://mariadb.com/kb/en/mariadb/dynamic-columns/) and is declared in the model class by the `dynamicColumn()` method. Everything else in a Dynamic AR class declaration is familiar AR stuff.

```
class Product extends \my6uot9\dynamicAr\DynamicActiveRecord
{
    public static function tableName()
    {
        return 'product';
    }

    public static function dynamicColumn() : string
    {
        return 'details';
    }
}
```

Now we can do all the normal AR things with `Product` but in addition we can read, write and update attributes not mentioned in the schema.

```
$product = new Product([
    'sku' => 5463,
    'upc' => '234569',
    'price' => 4.99,
    'title' => 'Clue-by-four',
    'description' => 'Used for larting lusers or constructing things',
    'dimensions' => [
        'unit' => 'inch',
        'width' => 4,
        'height' => 2,
        'length' => 20,
    ],
    'material' => 'wood',
]);
$product->save();
```

Think of the `details` table column as holding a serialized associative array. But unlike saving a JSON document in a text field, you can use dynamic attributes anywhere in your code, including in queries, just as you do with schema attributes. The differences are

- Nested attributes use dotted notation, e.g. `dimensions.length`
- Direct get and set of nested attributes on a model instance use the `getAttribute()`and `setAttribute()` methods because PHP doesn't allow dotted notation in identifiers.
- When a dynamic attribute appears in a query, wrap it in bang-parens `(! … !)`, e.g. `(! dimensions.length !)`. (Space between attribute name and its bang-parens is optional so `(!material!)` is fine.)

For example

```
$model = new Product([
    'title' => 'Car',
    'specs.fuel.tank.capacity' => 50,
    'specs.fuel.tank.capacity.unit' => 'liter',
]);
$model->setAttribute('specs.wheels.count', 4);
$model = Product::find()->where(['(!dimensions.length!)' => 10]);
$section = Product::find()
    ->select('CONCAT((! dimensions.width !), " x ", (! dimensions.height !))')
    ->where(['id' => 11])
    ->one();
```

The dot notation works anywhere Yii accepts an attribute name string, for example

```
class Product extends \my6uot9\dynamicAr\DynamicActiveRecord
{
    public function rules()
    {
        return [['dimensions.length', 'double', 'min' => 0.0]];
    }

    public function search($params)
    {
        $dataProvider = new ActiveDataProvider([
            'sort' => [
                'attributes' => [
                    'dimensions.length' => [
                        'asc' => ['(! dimensions.length !)' => SORT_DESC],
                        'desc' => ['(! dimensions.length !)' => SORT_ASC],
                    ],
                ],
            ],
            // ...
        ]);
    }

    public static function dynamicColumn() : string
    {
        return 'details';
    }
}
```

Design principle
----------------

[](#design-principle)

DynamicActiveRecord adds a fourth to the three things that reading and writing AR model properties can do:

1. `$model->foo` accesses, if it exists, the instance variable `$foo`,
2. otherwise it accesses the column attribute `foo`, if the model's table has a column "foo",
3. otherwise it accesses the virtual attribute `foo`, if the model's class has magic `getFoo()` / `setFoo()` methods,
4. else `$model->foo` accesses a dynamic attribute named "foo".

So any attribute name that doesn't refer to one of the normal 3 kinds of AR model property (instance variable, column attribute, virtual attribute) is automatically a dynamic property as soon as you use it. There is no way to declare a dynamic property and you can only define one by writing to it.

And reading an attribute that doesn't exist returns null.

#### PHP null, SQL NULL and Maria

[](#php-null-sql-null-and-maria)

Maria does not encode a dynamic column set to SQL NULL:

```
SELECT COLUMN_CREATE('a', 1, 'b', null) = COLUMN_CREATE('a', 1);
>> 1
```

Thus if a table record currently has a dynamic column 'b' and Maria executes an update setting it to NULL then Maria removes 'b' from the record. (This makes sense if NULL has its conventional database meaning of 'data value does not exist.') So DynamicActiveRecord cannot possibly distinguish a NULL value from a dynamic column that doesn't exist after reading back from the DB.

In order to be consistent, DynamicActiveRecord always returns null when you read a dynamic attribute that hasn't been set, in contrast to ActiveRecord which throws an exception. But it also makes sense if null means 'does not exist' and given the design principle (above).

Further reading
---------------

[](#further-reading)

Class reference

- [Docs landing page](http://tom--.github.io/yii2-dynamic-ar/)
- [DynamciActiveRecord](http://tom--.github.io/yii2-dynamic-ar/spinitron-dynamicar-dynamicactiverecord.html)
- [DynamicActiveQuery](http://tom--.github.io/yii2-dynamic-ar/spinitron-dynamicar-dynamicactivequery.html)
- [ValueExpression](http://tom--.github.io/yii2-dynamic-ar/spinitron-dynamicar-valueexpression.html)

More documentation

- [Datatypes](http://tom--.github.io/yii2-dynamic-ar/doc-datatypes.html) in PHP, SQL and JSON are not identical.
- [Design history](http://tom--.github.io/yii2-dynamic-ar/doc-design.html) – The projects original README.

Useful links

- [yii2-dynamic-ar project repo](https://github.com/tom--/dynamic-ar)
- [Yii 2 Framework](http://www.yiiframework.com/doc-2.0/guide-index.html)
- [Active Record guide](http://www.yiiframework.com/doc-2.0/guide-db-active-record.html)
- [Query Builder guide](http://www.yiiframework.com/doc-2.0/guide-db-query-builder.html)
- [Maria Dynamic Columns](https://mariadb.com/kb/en/mariadb/dynamic-columns/)
- [Sequel Pro Dynamic Columns bundle](https://github.com/tom--/sequel-pro-maria-dynamic-column)

Regenerate docs in gh-pages branch

```
vendor/bin/apidoc api . . --template="spinitron\dynamicAr\doc\template\ApiRenderer"

```

Questions, comments, issues
---------------------------

[](#questions-comments-issues)

Use the [issue tracker](https://github.com/tom--/dynamic-ar/issues).

---

Copyright (c) 2015 Spinitron LLC
Copyright 2021 My6UoT9

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity3

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity62

Established project with proven stability

 Bus Factor1

Top contributor holds 55.8% 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 ~162 days

Recently: every ~184 days

Total

14

Last Release

1904d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/6641cc5dec375c786160779b5e467238c30633563a8abc16f9771c79ad6a305c?d=identicon)[fDjKuIzZG](/maintainers/fDjKuIzZG)

---

Top Contributors

[![tom--](https://avatars.githubusercontent.com/u/1178722?v=4)](https://github.com/tom-- "tom-- (53 commits)")[![diego-mathis](https://avatars.githubusercontent.com/u/22345162?v=4)](https://github.com/diego-mathis "diego-mathis (39 commits)")[![cebe](https://avatars.githubusercontent.com/u/189796?v=4)](https://github.com/cebe "cebe (2 commits)")[![mata-man](https://avatars.githubusercontent.com/u/2185864?v=4)](https://github.com/mata-man "mata-man (1 commits)")

---

Tags

ormmariadbyii2activerecordarMariaDynamic Columns

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/my6uot9-yii2-dynamic-ar/health.svg)

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

###  Alternatives

[spinitron/yii2-dynamic-ar

Extends Yii ActiveRecord for Maria Dynamic Columns

576.8k](/packages/spinitron-yii2-dynamic-ar)[voskobovich/yii2-linker-behavior

This behavior makes it easy to maintain many-to-many and one-to-many relations in your ActiveRecord models.

80319.0k9](/packages/voskobovich-yii2-linker-behavior)[nhkey/yii2-activerecord-history

Storage history of changes to ActiveRecord

46143.4k1](/packages/nhkey-yii2-activerecord-history)[tommyknocker/pdo-database-class

Framework-agnostic PHP database library with unified API for MySQL, MariaDB, PostgreSQL, SQLite, MSSQL, and Oracle. Query Builder, caching, sharding, window functions, CTEs, JSON, migrations, ActiveRecord, CLI tools, AI-powered analysis. Zero external dependencies.

845.7k](/packages/tommyknocker-pdo-database-class)[jlorente/yii2-activerecord-inheritance

ActiveRecord Inheritance is an util to provide the Class Table Inheritance Pattern the to the Yii2 framework. It fakes inheritance between two ActiveRecord classes.

184.2k](/packages/jlorente-yii2-activerecord-inheritance)[execut/yii2-1c-odata

Yii2 component for work with 1C oData via activeRecord

101.3k](/packages/execut-yii2-1c-odata)

PHPackages © 2026

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