This is a copy of the project's README in Github.
These features are useful for representing entities with properties that don't comfortably map to table columns, for example because:
The classic example is product descriptions in a vendor's database. Washing machines need to be described with properties relevant to them (capacity, spin speed, power) while cameras require other properties. Imagine the set of properties for a general shopping website with a wide range of product types. Now imagine that product and product types need to be changed frequently.
Such entities are easily represented in a NoSQL document store such as CouchDB or MongoDB. But until recently representing them in relational DB tables typically means choosing a method that involves difficult compromise:
However, Maria 10 and PostgreSQL 9.4 have the ability to serialize an arbitrary set of (structured) properties while allowing them to be used in queries. This suggests the possibility of having the best of both worlds (RDBMS and document store) in one place.
The goal of yii2-dynamic-ar extension is to provide a comfortable API to these capabilities.
DynamicActiveRecord extends ActiveRecord to represent structured dynamic attributes that are stored in serialized form in the database. If the particular DBMS has features to support this then they are used, otherwise JSON is used. In the case that the DBMS allows querying of the serialized properties DynamicActiveQuery extends Active Query to represent such queries.
At present DynamicActiveRecord works only with Maria 10. Other DBMSs and plain JSON to come.
In a DynamicActiveRecord object, any property you access is a dynamic attribute when:
$product is an instance of a
DynamicActiveRecord class then
$product->color = 'red';
will write to:
$product->setColor('red'), if you declared this method,
Dynamic attributes can have array values, e.g.
$product->price = [ 'retail' => 12.99, 'wholesale' => [ 6 => 12.00, 12 => 11.50, 60 => 10.40, ], ]
A dynamic attribute's name must be a valid PHP label but child labels can be any non-empty string.
All dynamic attributes are populated into a model object from the DB when loading a model, regardless of the query's
When updating a model, the DB record's dynamic attributes are all overwritten. Another way to think about this is – after save, whether it is an insert or update, the DB record has the same set of dynamic attributes as the model. For example, let's say the record with PK of 7 in the
product table has a dynamic field 'speed'. If we now save a
Product model with
id 7 but without a dynamic attribute 'speed' then the dynamic field 'speed' 'is deleted from row 7 in the table.
tl;dr When you use a dynamic attribute in a query, write it as a dynamic attribute token, e.g.
(!employee.id|INT!) (see below for full details).
DynamicActiveQuery only exists for those DMBSs that provide a way to query elements in data structures serialized to a field (for now, Maria 10+ and PostgreSQL 9.4+).
We would like to be able to use dynamic attributes in database queries with the same flexibility as we have with schema column attributes. DynamicActiveQuery should allow, for example, creation of SQL such as the following, whether the columns involved are serialized dynamic properties or table columns in the schema:
SELECT t1.price, CONCAT('Compare at $', t1.price) AS foo, MIN(t1.price, t1.discount) AS sale FROM t1 JOIN t2 ON MIN(t1.price, t1.discount) > t2.cost WHERE price * 1.33 < 50.00 ORDER BY MIN(0.66 * t1.price, t2.cost)
There is a challenge. From the Maria manual:
SQL is a statically-typed language. The SQL interpreter needs to know the datatypes often all expressions before the query is run (for example, when one is using prepared statements and runs [a SELECT], the prepared statement API requires the server to inform the client about the datatype of the column being read before the query is executed and the server can see what datatype the column actually has).
So DynamicActiveQuery needs a way to:
The first cannot in general be automated by our extension without constraining the requirements or by radically changing ActiveQuery. Automating the second seems intractable to me. So I decided instead to require the user to distinguish dynamic attributes and declare their type. I feel this is reasonable since the user must always know both when accessing a dynamic attribute. (This is actually the basic nature of NoSQL – the schema is implicit in the application's business logic.) For this I invented a notation for dynamic attribute tokens in DynamicActiveQuery.
The token is wrapped in curly braces containing the dynamic attribute name and optionally its type. Type may be omitted, in which case it defaults to
CHAR, otherwise it is one of the datatypes allowed by the respective DBMS. Hierarchy in structured data is represented with dot separators:
Examples in queries
$blackShirts = Product::find() ->where(['category' => Product::SHIRT, '(!color!)' => 'black']) ->all(); $cheapShirts = Product::find() ->where(['category' => Product::SHIRT]) ->andWhere('(!price.wholesale.12|DECIMAL(6,2)!) < 20.00') ->select(['sale' => 'CONCAT("On sale at $", (!price.discount!))']) ->all();
tl;dr While you're unlikely to lose data, you might not get the same datatype back that you put in. Use your understanding of the attribute in the context of your app to decide how to handle it after loading a model. When you want to use an attribute in a query, you have to specify its datatype.
The following discusses the details.
Types are a bit of a muddle and you need to take care. Currently, for all DBMSs except Maria, dynamic attribute are serialized and unserialized PHP's
json_decode(). The same datatype conversion considerations and corner cases apply as in any use of JSON serialization, e.g. JSON has no integers, JSON arrays are different from PHP's, etc.
In Maria the situation is worse. Data is saved via SQL and retrieved via JSON. This may seem perverse but here's the logic.
This is better than nothing and should be tolerable in a lot of cases but it is a bit weird and introduces a number of considerations regarding datatypes through the lifecycle of an AR model/record.
So the thing to remember:
With respect to specific types...
SQL doesn't have float but has integer and decimal. JSON has only number, which is decimal and almost always an IEEE floating point double without NaN. PHP has integer and IEEE floats. This can get tricky but there is nothing really new here.
SQL doesn't have any such thing but JSON does. Maria (SQL) converts to int 0 or 1 on save. DMBS using JSON retain boolean type.
Maria does not save a dynamic column with a null value, thus
SELECT COLUMN_CREATE('a', 1, 'b', null) = COLUMN_CREATE('a', 1); >> 1
$product->foo = null; $product->save(); actually deletes dynamic attribute 'foo' in the corresponding DB record.
This is perfectly reasonable. The meaning and purpose of SQL NULL makes no sense for dynamic fields. The 'x' not existing in the record a better representation for 'x' not existing than a serialized data element with the funny NULL value/type.
So, even though JSON can adequately represent a PHP null, DynamicActiveRecord does promise to return a PHP null if you try to save one. If you do $x = null then PHP considers $x to be "unset".
DynamicActiveRecord saves PHP arrays such that they are associative on load. In other words, you may as well use string keys because they will be strings on load and they need to be strings when dynamic attribute names are used in queries.
DynamicActiveRecord converts to PHP array before save so you'd be better of not using PHP objects.
Copyright (c) 2015 Spinitron LLC