Download the PHP package my6uot9/yii2-dynamic-ar without Composer
On this page you can find all versions of the php package my6uot9/yii2-dynamic-ar. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download my6uot9/yii2-dynamic-ar
More information about my6uot9/yii2-dynamic-ar
Files in my6uot9/yii2-dynamic-ar
Package yii2-dynamic-ar
Short Description Extends Yii ActiveRecord for Maria Dynamic Columns
License ISC
Informations about the package yii2-dynamic-ar
Dynamic Active Record
This is a fork of Tom Worster's version.
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.
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.
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.
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()
andsetAttribute()
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
The dot notation works anywhere Yii accepts an attribute name string, for example
Design principle
DynamicActiveRecord adds a fourth to the three things that reading and writing AR model properties can do:
$model->foo
accesses, if it exists, the instance variable$foo
,- otherwise it accesses the column attribute
foo
, if the model's table has a column "foo", - otherwise it accesses the virtual attribute
foo
, if the model's class has magicgetFoo()
/setFoo()
methods, - 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:
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
- Datatypes in PHP, SQL and JSON are not identical.
- Design history – The projects original README.
Useful links
- yii2-dynamic-ar project repo
- Yii 2 Framework
- Active Record guide
- Query Builder guide
- Maria Dynamic Columns
- Sequel Pro Dynamic Columns bundle
Regenerate docs in gh-pages branch
vendor/bin/apidoc api . . --template="spinitron\dynamicAr\doc\template\ApiRenderer"
Questions, comments, issues
Use the issue tracker.
Copyright (c) 2015 Spinitron LLC
Copyright 2021 My6UoT9