Scalable Database Schema
Key-Value storage is a very simplistic, but very powerful model.
Use this behavior to expand your Yii 2 model without changing the structure., (*1)
Data can be queried and saved with "virtual attributes".
These are stored serialized in a configured table column., (*2)
Installation
The preferred way to install this extension is through composer., (*3)
Either run, (*4)
php composer.phar require --prefer-dist cakebake/yii2-scalable-behavior "*"
or add, (*5)
"cakebake/yii2-scalable-behavior": "*"
to the require section of your composer.json
file., (*6)
Preparation
Create a column in your desired table. It is recommended to use the type "text" or "longtext"
in order to save as much data as possible., (*7)
Usage
Once the extension is installed, simply use it in your model by adding:, (*8)
use cakebake\behaviors\ScalableBehavior;
public function behaviors()
{
return [
...
'scaleable' => [
'class' => ScalableBehavior::className(),
'scalableAttribute' => 'value', // The owner object's attribute / the column of the corresponding table, which are used as storage for the virtual attributes
'virtualAttributes' => ['about_me', 'birthday'] // Definition of virtual attributes that are added to the owner object
],
...
];
}
Now we can proceed similarly with the virtual attributes like normal., (*9)
public function rules()
{
return [
['about_me', 'required'],
['about_me', 'string'],
['birthday', 'string', 'max' => 60],
];
}
Piece of advice
This technique should be used only for metadata.
Improper use may change the application performance negatively., (*10)