forked from vova07/yii2-start-comments-module
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Model.php
executable file
·176 lines (154 loc) · 3.96 KB
/
Model.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
namespace stepancher\comments\models;
use stepancher\comments\Module;
use Yii;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\helpers\ArrayHelper;
/**
* This is the model class for table "{{%comments_models}}".
*
* @property integer $id ID
* @property string $name Model class name
* @property integer $status_id Status
* @property integer $created_at Created time
* @property integer $updated_at Updated time
*
* @property Comment[] $comments Comments
*/
class Model extends ActiveRecord
{
/** Status disabled */
const STATUS_DISABLED = 0;
/** Status enabled */
const STATUS_ENABLED = 1;
/** Model array cache key */
const CACHE_MODEL_ARRAY = 'cache-model-array';
/**
* @inheritdoc
*/
public static function tableName()
{
return '{{%comments_models}}';
}
/**
* @inheritdoc
*/
public function behaviors()
{
return [
'timestampBehavior' => [
'class' => TimestampBehavior::className()
]
];
}
/**
* Find model by ID.
*
* @param string|integer $id Model ID
*
* @return static|null Found model
*/
public static function findIdentity($id)
{
$id = is_numeric($id) ? $id : sprintf("%u", crc32($id));
return self::findOne($id);
}
/**
* @return array Status array
*/
public static function getStatusArray()
{
return [
self::STATUS_DISABLED => Module::t('comments-models', 'STATUS_DISABLED'),
self::STATUS_ENABLED => Module::t('comments-models', 'STATUS_ENABLED')
];
}
/**
* @return string Model readable status
*/
public function getStatus()
{
return self::getStatusArray()[$this->status_id];
}
/**
* @return array Model array
*/
public static function getModelArray()
{
if (($array = Yii::$app->cache->get(self::CACHE_MODEL_ARRAY)) === false) {
$array = ArrayHelper::map(self::find()->asArray()->all(), 'id', 'name');
Yii::$app->cache->set(self::CACHE_MODEL_ARRAY, $array);
}
return $array;
}
/**
* @inheritdoc
*/
public function rules()
{
return [
// Required
['name', 'required'],
// String
['name', 'string', 'max' => 255],
// Name
['name', 'unique'],
// Status
['status_id', 'in', 'range' => array_keys(self::getStatusArray())],
['status_id', 'default', 'value' => self::STATUS_ENABLED]
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => Module::t('comments-models', 'ATTR_ID'),
'name' => Module::t('comments-models', 'ATTR_NAME'),
'status_id' => Module::t('comments-models', 'ATTR_STATUS'),
'created_at' => Module::t('comments-models', 'ATTR_CREATED'),
'updated_at' => Module::t('comments-models', 'ATTR_UPDATED'),
];
}
/**
* @inheritdoc
*/
public function beforeSave($insert)
{
if (parent::beforeSave($insert)) {
$this->id = sprintf("%u", crc32($this->name));
return true;
} else {
return false;
}
}
/**
* @inheritdoc
*/
public function afterSave($insert, $changedAttributes)
{
parent::afterSave($insert, $changedAttributes);
Yii::$app->cache->delete(self::CACHE_MODEL_ARRAY);
}
/**
* @inheritdoc
*/
public function beforeDelete()
{
if (parent::beforeDelete()) {
Comment::deleteAll(['model_class' => $this->id]);
return true;
} else {
return false;
}
}
/**
* @return \yii\db\ActiveQuery
*/
public function getComments()
{
return $this->hasMany(Comment::className(), ['model_class' => 'id']);
}
}