/
Term.php
120 lines (110 loc) · 2.78 KB
/
Term.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
<?php
/**
* @link http://www.writesdown.com/
* @copyright Copyright (c) 2015 WritesDown
* @license http://www.writesdown.com/license/
*/
namespace common\models;
use Yii;
use yii\behaviors\SluggableBehavior;
use yii\db\ActiveRecord;
/**
* This is the model class for table "{{%term}}".
*
* @property integer $id
* @property integer $taxonomy_id
* @property string $name
* @property string $slug
* @property string $description
* @property integer $parent
* @property integer $count
* @property string $url
*
* @property Taxonomy $taxonomy
* @property TermRelationship[] $termRelationships
* @property Post[] $posts
*
* @author Agiel K. Saputra <13nightevil@gmail.com>
* @since 0.1.0
*/
class Term extends ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return '{{%term}}';
}
/**
* @inheritdoc
*/
public function behaviors()
{
return [
[
'class' => SluggableBehavior::className(),
'attribute' => 'name',
'attributes' => [ActiveRecord::EVENT_BEFORE_INSERT => ['slug']],
],
];
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['taxonomy_id', 'name'], 'required'],
[['taxonomy_id', 'parent', 'count'], 'integer'],
['description', 'string'],
[['name', 'slug'], 'string', 'max' => 200],
['name', 'unique'],
['slug', 'unique'],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => Yii::t('writesdown', 'ID'),
'taxonomy_id' => Yii::t('writesdown', 'Taxonomy ID'),
'name' => Yii::t('writesdown', 'Name'),
'slug' => Yii::t('writesdown', 'Slug'),
'description' => Yii::t('writesdown', 'Description'),
'parent' => Yii::t('writesdown', 'Parent'),
'count' => Yii::t('writesdown', 'Count'),
];
}
/**
* @return \yii\db\ActiveQuery
*/
public function getTaxonomy()
{
return $this->hasOne(Taxonomy::className(), ['id' => 'taxonomy_id']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getTermRelationships()
{
return $this->hasMany(TermRelationship::className(), ['term_id' => 'id']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getPosts()
{
return $this->hasMany(Post::className(), ['id' => 'post_id'])
->viaTable('{{%term_relationship}}', ['term_id' => 'id']);
}
/**
* Get URL of current term
*/
public function getUrl()
{
return Yii::$app->urlManagerFront->createAbsoluteUrl(['/term/view', 'id' => $this->id]);
}
}