-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStorageActiveRecord.php
127 lines (106 loc) · 3.01 KB
/
StorageActiveRecord.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
namespace yii1tech\config;
/**
* StorageActiveRecord is an configuration storage based on ActiveRecord.
*
* Example migration for ActiveRecord table:
*
* ```php
* $tableName = 'app_config';
* $columns = [
* 'id' => 'pk',
* 'key' => 'string',
* 'value' => 'text',
* 'PRIMARY KEY(id)',
* ];
* $this->createTable($tableName, $columns);
* ```
*
* @see \CActiveRecord
*
* @author Paul Klimov <[email protected]>
* @since 1.0
*/
class StorageActiveRecord extends Storage
{
/**
* @var string|\CActiveRecord name of the ActiveRecord model class, which should store the config values.
*/
public $model;
/**
* @var string name of the model attribute, which should store config item key.
*/
public $keyAttribute = 'key';
/**
* @var string name of the model attribute, which should store config item value.
*/
public $valueAttribute = 'value';
/**
* {@inheritdoc}
*/
public function save(array $values): bool
{
$existingModels = $this->createModelFinder()->findAll();
$result = true;
foreach ($existingModels as $key => $existingModel) {
if (array_key_exists($existingModel->{$this->keyAttribute}, $values)) {
$existingModel->value = $values[$existingModel->{$this->keyAttribute}];
$result = $result && $existingModel->save(false);
unset($values[$existingModel->{$this->keyAttribute}]);
unset($existingModels[$key]);
}
}
foreach ($values as $key => $value) {
/* @var $model \CActiveRecord */
$model = new $this->model();
$attributes = [$this->keyAttribute => $key, $this->valueAttribute => $value];
foreach ($attributes as $attributeName => $attributeValue) {
$model->{$attributeName} = $attributeValue;
}
$result = $result && $model->save(false);
}
return $result;
}
/**
* {@inheritdoc}
*/
public function get(): array
{
$result = [];
foreach ($this->createModelFinder()->findAll() as $model) {
$result[$model->{$this->keyAttribute}] = $model->{$this->valueAttribute};
}
return $result;
}
/**
* {@inheritdoc}
*/
public function clear(): bool
{
$result = true;
foreach ($this->createModelFinder()->findAll() as $model) {
$result = $result && $model->delete();
}
return $result;
}
/**
* {@inheritdoc}
*/
public function clearValue($key): bool
{
$model = $this->createModelFinder()
->findByAttributes([$this->keyAttribute => $key]);
if (!empty($model)) {
return $model->delete();
}
return true;
}
/**
* @return \CActiveRecord model finder instance.
*/
protected function createModelFinder()
{
$class = $this->model;
return $class::model($class);
}
}