1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link http://www.writesdown.com/ |
4
|
|
|
* @copyright Copyright (c) 2015 WritesDown |
5
|
|
|
* @license http://www.writesdown.com/license/ |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace common\models; |
9
|
|
|
|
10
|
|
|
use Yii; |
11
|
|
|
use yii\behaviors\SluggableBehavior; |
12
|
|
|
use yii\db\ActiveRecord; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* This is the model class for table "{{%term}}". |
16
|
|
|
* |
17
|
|
|
* @property integer $id |
18
|
|
|
* @property integer $taxonomy_id |
19
|
|
|
* @property string $name |
20
|
|
|
* @property string $slug |
21
|
|
|
* @property string $description |
22
|
|
|
* @property integer $parent |
23
|
|
|
* @property integer $count |
24
|
|
|
* @property string $url |
25
|
|
|
* |
26
|
|
|
* @property Taxonomy $taxonomy |
27
|
|
|
* @property TermRelationship[] $termRelationships |
28
|
|
|
* @property Post[] $posts |
29
|
|
|
* |
30
|
|
|
* @author Agiel K. Saputra <[email protected]> |
31
|
|
|
* @since 0.1.0 |
32
|
|
|
*/ |
33
|
|
|
class Term extends ActiveRecord |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* @inheritdoc |
37
|
|
|
*/ |
38
|
|
|
public static function tableName() |
39
|
|
|
{ |
40
|
|
|
return '{{%term}}'; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @inheritdoc |
45
|
|
|
*/ |
46
|
|
View Code Duplication |
public function behaviors() |
|
|
|
|
47
|
|
|
{ |
48
|
|
|
return [ |
49
|
|
|
[ |
50
|
|
|
'class' => SluggableBehavior::className(), |
51
|
|
|
'attribute' => 'name', |
52
|
|
|
'attributes' => [ActiveRecord::EVENT_BEFORE_INSERT => ['slug']], |
53
|
|
|
], |
54
|
|
|
]; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @inheritdoc |
59
|
|
|
*/ |
60
|
|
View Code Duplication |
public function rules() |
|
|
|
|
61
|
|
|
{ |
62
|
|
|
return [ |
63
|
|
|
[['taxonomy_id', 'name'], 'required'], |
64
|
|
|
[['taxonomy_id', 'parent', 'count'], 'integer'], |
65
|
|
|
['description', 'string'], |
66
|
|
|
[['name', 'slug'], 'string', 'max' => 200], |
67
|
|
|
['name', 'unique'], |
68
|
|
|
['slug', 'unique'], |
69
|
|
|
]; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @inheritdoc |
74
|
|
|
*/ |
75
|
|
View Code Duplication |
public function attributeLabels() |
|
|
|
|
76
|
|
|
{ |
77
|
|
|
return [ |
78
|
|
|
'id' => Yii::t('writesdown', 'ID'), |
79
|
|
|
'taxonomy_id' => Yii::t('writesdown', 'Taxonomy ID'), |
80
|
|
|
'name' => Yii::t('writesdown', 'Name'), |
81
|
|
|
'slug' => Yii::t('writesdown', 'Slug'), |
82
|
|
|
'description' => Yii::t('writesdown', 'Description'), |
83
|
|
|
'parent' => Yii::t('writesdown', 'Parent'), |
84
|
|
|
'count' => Yii::t('writesdown', 'Count'), |
85
|
|
|
]; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return \yii\db\ActiveQuery |
90
|
|
|
*/ |
91
|
|
|
public function getTaxonomy() |
92
|
|
|
{ |
93
|
|
|
return $this->hasOne(Taxonomy::className(), ['id' => 'taxonomy_id']); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return \yii\db\ActiveQuery |
98
|
|
|
*/ |
99
|
|
|
public function getTermRelationships() |
100
|
|
|
{ |
101
|
|
|
return $this->hasMany(TermRelationship::className(), ['term_id' => 'id']); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return \yii\db\ActiveQuery |
106
|
|
|
*/ |
107
|
|
|
public function getPosts() |
108
|
|
|
{ |
109
|
|
|
return $this->hasMany(Post::className(), ['id' => 'post_id']) |
110
|
|
|
->viaTable('{{%term_relationship}}', ['term_id' => 'id']); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Get URL of current term |
115
|
|
|
*/ |
116
|
|
|
public function getUrl() |
117
|
|
|
{ |
118
|
|
|
return Yii::$app->urlManagerFront->createAbsoluteUrl(['/term/view', 'id' => $this->id]); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
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.