yii2-dynamic-ar

An extension to add NoSQL-like documents to Yii 2 Framework's Active Record ORM.

  • 所有者: tom--/yii2-dynamic-ar
  • 平台:
  • 許可證: ISC License
  • 分類:
  • 主題:
  • 喜歡:
    0
      比較:

Github星跟蹤圖

Dynamic Active Record

The yii2-dynamic-ar extension adds NoSQL-like documents to
Yii 2 Framework's
Active Record ORM.

Maria Dynamic Columns and PostgreSQL jsonb

Dynamic Columns
in Maria 10.0+
and jsonb column types
and functions in
in PostgreSQL 9.4+
provide, in effect, a NoSQL document
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
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

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 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 \spinitron\dynamicAr\DynamicActiveRecord
{
    public static function tableName()
    {
        return 'product';
    }

    public static function dynamicColumn()
    {
        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 \spinitron\dynamicAr\DynamicActiveRecord
{
    public function rules()
    {
        return ;
    }

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

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

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

Class reference

More documentation

Useful links

Regenerate docs in gh-pages branch

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

Questions, comments, issues

Use the issue tracker. Or you can easily find my email if you prefer.


Copyright (c) 2015 Spinitron LLC

主要指標

概覽
名稱與所有者tom--/yii2-dynamic-ar
主編程語言PHP
編程語言PHP (語言數: 1)
平台
許可證ISC License
所有者活动
創建於2015-01-25 13:48:21
推送於2019-02-17 01:49:32
最后一次提交2019-02-17 09:48:52
發布數10
最新版本名稱0.3.3 (發布於 )
第一版名稱0.1.0 (發布於 )
用户参与
星數57
關注者數17
派生數15
提交數179
已啟用問題?
問題數19
打開的問題數13
拉請求數4
打開的拉請求數1
關閉的拉請求數4
项目设置
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?