1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace app\modules\page\models; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use yii\behaviors\TimestampBehavior; |
7
|
|
|
use yii\db\ActiveRecord; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* This is the model class for table "pages". |
11
|
|
|
* |
12
|
|
|
* @property integer $id |
13
|
|
|
* @property string $key |
14
|
|
|
* @property string $title |
15
|
|
|
* @property string $content |
16
|
|
|
* @property string $description |
17
|
|
|
* @property string $created_at |
18
|
|
|
* @property string $updated_at |
19
|
|
|
*/ |
20
|
|
|
class Page extends ActiveRecord |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @inheritdoc |
24
|
|
|
*/ |
25
|
4 |
View Code Duplication |
public function behaviors() |
|
|
|
|
26
|
|
|
{ |
27
|
|
|
return [ |
28
|
|
|
'timestamp' => [ |
29
|
4 |
|
'class' => TimestampBehavior::className(), |
30
|
|
|
'attributes' => [ |
31
|
4 |
|
ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'], |
32
|
|
|
], |
33
|
4 |
|
'value' => date('Y-m-d H:i:s'), |
34
|
4 |
|
], |
35
|
|
|
]; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @inheritdoc |
40
|
|
|
*/ |
41
|
6 |
|
public static function tableName() |
42
|
|
|
{ |
43
|
6 |
|
return 'pages'; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @inheritdoc |
48
|
|
|
*/ |
49
|
1 |
|
public function rules() |
50
|
|
|
{ |
51
|
|
|
return [ |
52
|
1 |
|
[['created_at', 'updated_at'], 'safe'], |
53
|
|
|
[['content'], 'string'], |
54
|
|
|
['title', 'string', 'max' => 64], |
55
|
|
|
[['key', 'description'], 'string', 'max' => 255], |
56
|
|
|
[['key'], 'unique'], |
57
|
|
|
]; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @inheritdoc |
62
|
|
|
*/ |
63
|
3 |
View Code Duplication |
public function attributeLabels() |
|
|
|
|
64
|
|
|
{ |
65
|
|
|
return [ |
66
|
3 |
|
'id' => Yii::t('page', 'ID'), |
67
|
3 |
|
'key' => Yii::t('page', 'Key'), |
68
|
3 |
|
'title' => Yii::t('page', 'Title'), |
69
|
3 |
|
'content' => Yii::t('page', 'Content'), |
70
|
3 |
|
'description' => Yii::t('page', 'Description'), |
71
|
3 |
|
'created_at' => Yii::t('page', 'Created At'), |
72
|
3 |
|
'updated_at' => Yii::t('page', 'Updated At'), |
73
|
|
|
]; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Find page by key |
78
|
|
|
* |
79
|
|
|
* @param $key |
80
|
|
|
* @return $this|null |
81
|
|
|
*/ |
82
|
3 |
|
public static function findByKey($key) |
83
|
|
|
{ |
84
|
3 |
|
return static::findOne(['key' => $key]); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.