1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace app\properties; |
4
|
|
|
|
5
|
|
|
use app\models\BaseObject; |
6
|
|
|
use devgroup\TagDependencyHelper\ActiveRecordHelper; |
7
|
|
|
use yii\behaviors\AttributeBehavior; |
8
|
|
|
use yii\db\ActiveRecord; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @property integer $id |
12
|
|
|
* @property integer $object_model_id |
13
|
|
|
* @property integer $property_group_id |
14
|
|
|
* @property string $key |
15
|
|
|
* @property string $value |
16
|
|
|
*/ |
17
|
|
|
class AbstractPropertyEavModel extends ActiveRecord |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var string|null Table name |
21
|
|
|
*/ |
22
|
|
|
static private $tableName = null; |
23
|
|
|
static private $objectClassMap = []; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @inheritdoc |
27
|
|
|
*/ |
28
|
|
|
public static function tableName() |
29
|
|
|
{ |
30
|
|
|
return '{{%'.static::$tableName.'}}'; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param string $tableName |
35
|
|
|
*/ |
36
|
|
|
public static function setTableName($tableName) |
37
|
|
|
{ |
38
|
|
|
static::$tableName = $tableName; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @inheritdoc |
43
|
|
|
*/ |
44
|
|
View Code Duplication |
public function behaviors() |
45
|
|
|
{ |
46
|
|
|
return [ |
47
|
|
|
[ |
48
|
|
|
'class' => AttributeBehavior::className(), |
|
|
|
|
49
|
|
|
'attributes' => [ |
50
|
|
|
\yii\db\ActiveRecord::EVENT_BEFORE_INSERT => 'sort_order', |
51
|
|
|
], |
52
|
|
|
'value' => 0, |
53
|
|
|
], |
54
|
|
|
]; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @inheritdoc |
59
|
|
|
*/ |
60
|
|
|
public function rules() |
61
|
|
|
{ |
62
|
|
|
return [ |
63
|
|
|
[['object_model_id', 'property_group_id', 'key'], 'required'], |
64
|
|
|
[['object_model_id', 'property_group_id'], 'integer'], |
65
|
|
|
[['key'], 'string', 'max' => 255], |
66
|
|
|
[['value'], 'string'], |
67
|
|
|
[['value'], 'default', 'value' => ''], |
68
|
|
|
]; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param integer|null $model_id |
73
|
|
|
* @param string|null $key |
74
|
|
|
* @param integer|null $property_group |
75
|
|
|
* @return \yii\db\ActiveRecord[]|array |
76
|
|
|
*/ |
77
|
|
|
public static function findByModelId($model_id = null, $key = null, $property_group = null) |
78
|
|
|
{ |
79
|
|
|
if (null === $model_id) { |
80
|
|
|
return []; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$query = static::find()->where(['object_model_id' => $model_id]); |
84
|
|
|
if (null !== $key) { |
85
|
|
|
$query->andWhere(['key' => $key]); |
86
|
|
|
} |
87
|
|
|
if (null !== $property_group) { |
88
|
|
|
$query->andWhere(['property_group_id' => $property_group]); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $query->all(); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @inheritdoc |
96
|
|
|
*/ |
97
|
|
|
public function afterDelete() |
98
|
|
|
{ |
99
|
|
|
parent::afterDelete(); |
100
|
|
|
$this->invalidateObjectCache(); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @inheritdoc |
105
|
|
|
*/ |
106
|
|
|
public function afterSave($insert, $changedAttributes) |
107
|
|
|
{ |
108
|
|
|
$this->invalidateObjectCache(); |
109
|
|
|
parent::afterSave($insert, $changedAttributes); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Invalidate cache for object |
114
|
|
|
*/ |
115
|
|
|
private function invalidateObjectCache() |
116
|
|
|
{ |
117
|
|
|
$className = null; |
118
|
|
|
if (!isset(static::$objectClassMap[static::$tableName])) { |
119
|
|
|
/** @var BaseObject $object */ |
120
|
|
|
if (null !== $object = BaseObject::findOne(['eav_table_name' => static::$tableName])) { |
121
|
|
|
static::$objectClassMap[static::$tableName] = $object->object_class; |
122
|
|
|
$className = static::$objectClassMap[static::$tableName]; |
123
|
|
|
} |
124
|
|
|
} else { |
125
|
|
|
$className = static::$objectClassMap[static::$tableName]; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
if (null !== $className) { |
129
|
|
|
\yii\caching\TagDependency::invalidate( |
130
|
|
|
\Yii::$app->cache, |
131
|
|
|
[ |
132
|
|
|
ActiveRecordHelper::getObjectTag($className, $this->object_model_id) |
133
|
|
|
] |
134
|
|
|
); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
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.