Changes
Title
unchanged
How to store array/widget configuration to the database with config validation rules
Category
unchanged
Tutorials
Yii version
unchanged
Tags
unchanged
yii2, dynamic, model, save, widget, config, database, db, store, array
Content
changed
[...]
Design
------
The design approach involves creating the data structure to store the serialized array configurations with tags as variables that will be replaced at runtime. The rules/validators for validating the widget configuration, will also be stored serialized in a rules column. While running the widget at runtime, the rules will be validated dynamically using the yii\base\DynamicModel.
### 1. Data Structure
Create a table `tbl_widget_config` in your database with the following minimum columns. Create a model called `WidgetConfig
` for this table through gii.
~~~[...]
// Retrieve the configuration
public function runWidget($configId) {
$dbWidget = WidgetConfig::findOne($configId)
->asArray();
$widgetConfig = \yii\base\DynamicModel::validateData(
static::parseTags($dbWidget
['->config
']),
static::parseTags($dbWidget
['->rules
'])
);
// To run the widget call below
$class = $dbWidget
['->class_name
'];
$class::widget($widgetConfig->getAttributes());
}
```