1
|
|
|
<?php |
2
|
|
|
namespace app\models; |
3
|
|
|
|
4
|
|
|
use app\modules\data\models\Export; |
5
|
|
|
use app\modules\data\models\Import; |
6
|
|
|
use Yii; |
7
|
|
|
use yii\caching\TagDependency; |
8
|
|
|
use yii\db\ActiveRecord; |
9
|
|
|
use yii\db\Query; |
10
|
|
|
use yii\helpers\ArrayHelper; |
11
|
|
|
use devgroup\TagDependencyHelper\ActiveRecordHelper; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* This is the model class for table "object". |
15
|
|
|
* |
16
|
|
|
* @property integer $id |
17
|
|
|
* @property string $name |
18
|
|
|
* @property string $object_class |
19
|
|
|
* @property string $object_table_name |
20
|
|
|
* @property string $column_properties_table_name |
21
|
|
|
* @property string $eav_table_name |
22
|
|
|
* @property string $categories_table_name |
23
|
|
|
* @property string $link_slug_category |
24
|
|
|
* @property string $link_slug_static_value |
25
|
|
|
*/ |
26
|
|
|
|
27
|
|
|
class BaseObject extends ActiveRecord implements \JsonSerializable |
28
|
|
|
{ |
29
|
|
|
private static $identity_map = []; |
30
|
|
|
private static $ids_for_class_name = []; |
31
|
|
|
private static $select_array_cache = null; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @inheritdoc |
35
|
|
|
*/ |
36
|
|
|
public function behaviors() |
37
|
|
|
{ |
38
|
|
|
return [ |
39
|
|
|
[ |
40
|
|
|
'class' => \devgroup\TagDependencyHelper\ActiveRecordHelper::className(), |
|
|
|
|
41
|
|
|
], |
42
|
|
|
]; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @inheritdoc |
47
|
|
|
*/ |
48
|
|
|
public static function tableName() |
49
|
|
|
{ |
50
|
|
|
return '{{%object}}'; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @inheritdoc |
55
|
|
|
*/ |
56
|
|
|
public function rules() |
57
|
|
|
{ |
58
|
|
|
return [ |
59
|
|
|
[ |
60
|
|
|
['name', 'object_class', 'object_table_name', 'column_properties_table_name', |
61
|
|
|
'eav_table_name', 'categories_table_name', 'link_slug_category', 'link_slug_static_value'], |
62
|
|
|
'required' |
63
|
|
|
], |
64
|
|
|
[ |
65
|
|
|
['name', 'object_class', 'object_table_name', 'column_properties_table_name', |
66
|
|
|
'eav_table_name', 'categories_table_name', 'link_slug_category', 'link_slug_static_value'], |
67
|
|
|
'string' |
68
|
|
|
] |
69
|
|
|
]; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @inheritdoc |
74
|
|
|
*/ |
75
|
|
|
public function attributeLabels() |
76
|
|
|
{ |
77
|
|
|
return [ |
78
|
|
|
'id' => Yii::t('app', 'ID'), |
79
|
|
|
'name' => Yii::t('app', 'Name'), |
80
|
|
|
'object_class' => Yii::t('app', 'Object Class'), |
81
|
|
|
'object_table_name' => Yii::t('app', 'Object Table Name'), |
82
|
|
|
'column_properties_table_name' => Yii::t('app', 'Column Properties Table Name'), |
83
|
|
|
'eav_table_name' => Yii::t('app', 'EAV Table Name'), |
84
|
|
|
'categories_table_name' => Yii::t('app', 'Categories Table Name'), |
85
|
|
|
'link_slug_category' => Yii::t('app', 'Link Slug Category'), |
86
|
|
|
'link_slug_static_value' => Yii::t('app', 'Link Slug Static Value'), |
87
|
|
|
]; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Returns model instance by ID using IdentityMap |
92
|
|
|
* @param integer $id |
93
|
|
|
* @return BaseObject|null |
94
|
|
|
*/ |
95
|
|
|
public static function findById($id) |
96
|
|
|
{ |
97
|
|
|
if (!isset(static::$identity_map[$id])) { |
98
|
|
|
$cacheKey = static::className() . ':' . $id; |
|
|
|
|
99
|
|
|
if (false !== $cache = Yii::$app->cache->get($cacheKey)) { |
100
|
|
|
return static::$identity_map[$id] = $cache; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$cache = static::findOne(['id' => $id]); |
104
|
|
|
if (null !== $cache) { |
105
|
|
|
static::$ids_for_class_name[$cache->object_class] = $id; |
106
|
|
|
Yii::$app->cache->set( |
107
|
|
|
$cacheKey, |
108
|
|
|
$cache, |
109
|
|
|
0, |
110
|
|
|
new TagDependency([ |
111
|
|
|
'tags' => [ |
112
|
|
|
ActiveRecordHelper::getObjectTag(static::className(), $id), |
|
|
|
|
113
|
|
|
] |
114
|
|
|
]) |
115
|
|
|
); |
116
|
|
|
} |
117
|
|
|
return static::$identity_map[$id] = $cache; |
|
|
|
|
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return static::$identity_map[$id]; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param string $class_name |
125
|
|
|
* @return null|\app\models\BaseObject |
|
|
|
|
126
|
|
|
*/ |
127
|
|
|
public static function getForClass($class_name) |
128
|
|
|
{ |
129
|
|
|
if (isset(static::$ids_for_class_name[$class_name])) { |
130
|
|
|
$id = static::$ids_for_class_name[$class_name]; |
131
|
|
|
return BaseObject::findById($id); |
132
|
|
|
} else { |
133
|
|
|
$object = Yii::$app->cache->get('ObjectByClassName: ' . $class_name); |
134
|
|
View Code Duplication |
if ($object === false) { |
135
|
|
|
$object = BaseObject::find() |
136
|
|
|
->where( |
137
|
|
|
[ |
138
|
|
|
'object_class' => $class_name, |
139
|
|
|
] |
140
|
|
|
)->one(); |
141
|
|
|
if ($object !== null) { |
142
|
|
|
Yii::$app->cache->set( |
143
|
|
|
'ObjectByClassName: ' . $class_name, |
144
|
|
|
$object, |
145
|
|
|
86400, |
146
|
|
|
new TagDependency( |
147
|
|
|
[ |
148
|
|
|
'tags' => [ |
149
|
|
|
\devgroup\TagDependencyHelper\ActiveRecordHelper::getObjectTag($object, $object->id), |
|
|
|
|
150
|
|
|
], |
151
|
|
|
] |
152
|
|
|
) |
153
|
|
|
); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
if (is_object($object)) { |
157
|
|
|
static::$identity_map[$object->id] = $object; |
158
|
|
|
static::$ids_for_class_name[$class_name] = $object->id; |
159
|
|
|
return static::$identity_map[$object->id]; |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
return null; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Возвращает список всех объектов |
167
|
|
|
* Ключ - ID |
168
|
|
|
* Значение - name |
169
|
|
|
* Используется для фильтрации в таблицах и выборе объекта в форме |
170
|
|
|
*/ |
171
|
|
View Code Duplication |
public static function getSelectArray() |
172
|
|
|
{ |
173
|
|
|
if (static::$select_array_cache === null) { |
174
|
|
|
static::$select_array_cache = Yii::$app->cache->get('ObjectsList'); |
175
|
|
|
if (static::$select_array_cache === false) { |
176
|
|
|
$rows = (new Query()) |
177
|
|
|
->select('id, name') |
178
|
|
|
->from(BaseObject::tableName()) |
179
|
|
|
->all(); |
180
|
|
|
static::$select_array_cache = ArrayHelper::map($rows, 'id', 'name'); |
181
|
|
|
} |
182
|
|
|
Yii::$app->cache->set( |
183
|
|
|
'ObjectsList', |
184
|
|
|
static::$select_array_cache, |
185
|
|
|
86400, |
186
|
|
|
new TagDependency( |
187
|
|
|
[ |
188
|
|
|
'tags' => [ |
189
|
|
|
\devgroup\TagDependencyHelper\ActiveRecordHelper::getCommonTag(static::className()), |
|
|
|
|
190
|
|
|
], |
191
|
|
|
] |
192
|
|
|
) |
193
|
|
|
); |
194
|
|
|
} |
195
|
|
|
return static::$select_array_cache; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
View Code Duplication |
public function getLastExport() |
199
|
|
|
{ |
200
|
|
|
return $this->hasOne(Export::className(), ['object_id' => 'id']) |
|
|
|
|
201
|
|
|
->andOnCondition( |
202
|
|
|
[ |
203
|
|
|
Export::tableName() . '.user_id' => Yii::$app->user->id, |
204
|
|
|
] |
205
|
|
|
); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
View Code Duplication |
public function getLastImport() |
209
|
|
|
{ |
210
|
|
|
return $this->hasOne(Import::className(), ['object_id' => 'id']) |
|
|
|
|
211
|
|
|
->andOnCondition( |
212
|
|
|
[ |
213
|
|
|
Import::tableName() . '.user_id' => Yii::$app->user->id, |
214
|
|
|
] |
215
|
|
|
); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* @return string |
220
|
|
|
*/ |
221
|
|
|
public function __toString() |
222
|
|
|
{ |
223
|
|
|
return ($this->className() . ':' . $this->id); |
|
|
|
|
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* @return string |
228
|
|
|
*/ |
229
|
|
|
public function jsonSerialize() |
230
|
|
|
{ |
231
|
|
|
return ($this->className() . ':' . $this->id); |
|
|
|
|
232
|
|
|
} |
233
|
|
|
} |
234
|
|
|
|
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.