City.php
3.09 KB
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
<?php
namespace thread\modules\location\models;
use Yii;
use yii\behaviors\AttributeBehavior;
use yii\helpers\ArrayHelper;
use yii\helpers\Inflector;
//
use thread\app\base\models\ActiveRecord;
use thread\modules\location\Location as LocationModule;
/**
* Class City
*
* @package thread\modules\location\models
* @author FilamentV <vortex.filament@gmail.com>
* @copyright (c), Thread
*/
class City extends ActiveRecord
{
/**
* @inheritdoc
*/
public static function getDb()
{
return LocationModule::getDb();
}
/**
* @return string
*/
public static function tableName()
{
return '{{%location_city}}';
}
/**
* @inheritdoc
* @return array
*/
public function behaviors()
{
return ArrayHelper::merge(
parent::behaviors(),
[
[
'class' => AttributeBehavior::className(),
'attributes' => [
ActiveRecord::EVENT_BEFORE_INSERT => 'alias',
ActiveRecord::EVENT_BEFORE_UPDATE => 'alias',
],
'value' => function ($event) {
return Inflector::slug($this->alias);
},
],
]
);
}
/**
* @return array
*/
public function rules()
{
return [
[['alias'], 'required'],
[['published', 'deleted'], 'in', 'range' => array_keys(static::statusKeyRange())],
[['country_id', 'created_at', 'updated_at'], 'integer'],
[['alias', 'default_title'], 'string', 'max' => 255],
[['alias'], 'unique'],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => Yii::t('app', 'ID'),
'alias' => Yii::t('app', 'Alias'),
'country_id' => Yii::t('location', 'Country'),
'created_at' => Yii::t('app', 'Create time'),
'updated_at' => Yii::t('app', 'Update time'),
'published' => Yii::t('app', 'Published'),
'deleted' => Yii::t('app', 'Deleted'),
'title' => Yii::t('app', 'Title'),
'default_title' => Yii::t('app', 'Default Title'),
];
}
/**
*
* @return array
*/
public function scenarios()
{
return [
'published' => ['published'],
'deleted' => ['deleted'],
'backend' => ['alias', 'country_id', 'created_at', 'updated_at', 'published', 'deleted', 'search_title', 'default_title'],
];
}
/**
* @return \yii\db\ActiveQuery
*/
public function getLang()
{
return $this->hasOne(CityLang::class, ['rid' => 'id']);
}
/**
* @return string
*/
public function getTitle()
{
return (isset($this->lang->title)) ? $this->lang->title : "{{$this->default_title}}";
}
/**
* @return \yii\db\ActiveQuery
*/
public function getCountry()
{
return $this->hasOne(Country::class, ['id' => 'country_id']);
}
}