|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace zacksleo\yii2\lookup\models; |
|
4
|
|
|
|
|
5
|
|
|
use Yii; |
|
6
|
|
|
use yii\behaviors\TimestampBehavior; |
|
7
|
|
|
use yii\db\ActiveRecord; |
|
8
|
|
|
use zacksleo\yii2\lookup\Module; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* This is the model class for table "lookup". |
|
12
|
|
|
* |
|
13
|
|
|
* @property integer $id |
|
14
|
|
|
* @property string $type |
|
15
|
|
|
* @property string $name |
|
16
|
|
|
* @property integer $code |
|
17
|
|
|
* @property string $comment |
|
18
|
|
|
* @property integer $active |
|
19
|
|
|
* @property integer $order |
|
20
|
|
|
* @property integer $created_at |
|
21
|
|
|
* @property integer $updated_at |
|
22
|
|
|
*/ |
|
23
|
|
|
class Lookup extends ActiveRecord |
|
24
|
|
|
{ |
|
25
|
|
|
private static $_items = []; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @inheritdoc |
|
29
|
|
|
*/ |
|
30
|
6 |
|
public static function tableName() |
|
31
|
|
|
{ |
|
32
|
6 |
|
return '{{%lookup}}'; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
6 |
|
public function behaviors() |
|
36
|
|
|
{ |
|
37
|
|
|
return [ |
|
38
|
6 |
|
TimestampBehavior::className(), |
|
39
|
6 |
|
]; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @inheritdoc |
|
44
|
|
|
*/ |
|
45
|
4 |
|
public function rules() |
|
46
|
|
|
{ |
|
47
|
|
|
return [ |
|
48
|
4 |
|
[['type', 'name', 'code', 'active', 'order'], 'required'], |
|
49
|
4 |
|
[['code', 'active', 'order', 'created_at', 'updated_at'], 'integer'], |
|
50
|
4 |
|
[['comment'], 'string'], |
|
51
|
4 |
|
[['type', 'name'], 'string', 'max' => 100], |
|
52
|
4 |
|
[['type', 'name'], 'unique', 'targetAttribute' => ['type', 'name'], 'message' => Module::t('core', 'The combination of Type and Name has already been taken.')] |
|
53
|
4 |
|
]; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @inheritdoc |
|
58
|
|
|
*/ |
|
59
|
1 |
|
public function attributeLabels() |
|
60
|
|
|
{ |
|
61
|
|
|
return [ |
|
62
|
1 |
|
'id' => Module::t('core', 'ID'), |
|
63
|
1 |
|
'type' => Module::t('core', 'Type'), |
|
64
|
1 |
|
'name' => Module::t('core', 'Name'), |
|
65
|
1 |
|
'code' => Module::t('core', 'Code'), |
|
66
|
1 |
|
'comment' => Module::t('core', 'Comment'), |
|
67
|
1 |
|
'active' => Module::t('core', 'Active'), |
|
68
|
1 |
|
'order' => Module::t('core', 'Order'), |
|
69
|
1 |
|
'created_at' => Module::t('core', 'Created At'), |
|
70
|
1 |
|
'updated_at' => Module::t('core', 'Updated At'), |
|
71
|
1 |
|
]; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Returns the items for the specified type. |
|
76
|
|
|
* @param string item type (e.g. 'PostStatus'). |
|
77
|
|
|
* @return array item names indexed by item code. The items are order by their order values. |
|
78
|
|
|
* An empty array is returned if the item type does not exist. |
|
79
|
|
|
*/ |
|
80
|
1 |
|
public static function items($type) |
|
81
|
|
|
{ |
|
82
|
1 |
|
if (!isset(self::$_items[$type])) { |
|
83
|
1 |
|
self::loadItems($type); |
|
84
|
1 |
|
} |
|
85
|
1 |
|
return self::$_items[$type]; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Returns the item name for the specified type and code. |
|
90
|
|
|
* @param string the item type (e.g. 'PostStatus'). |
|
91
|
|
|
* @param integer the item code (corresponding to the 'code' column value) |
|
92
|
|
|
* @return string the item name for the specified the code. False is returned if the item type or code does not exist. |
|
93
|
|
|
*/ |
|
94
|
1 |
|
public static function item($type, $code) |
|
95
|
|
|
{ |
|
96
|
1 |
|
if (!isset(self::$_items[$type])) { |
|
97
|
1 |
|
self::loadItems($type); |
|
98
|
1 |
|
} |
|
99
|
1 |
|
return isset(self::$_items[$type][$code]) ? self::$_items[$type][$code] : false; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Loads the lookup items for the specified type from the database. |
|
104
|
|
|
* @param string the item type |
|
105
|
|
|
*/ |
|
106
|
2 |
|
private static function loadItems($type) |
|
107
|
|
|
{ |
|
108
|
2 |
|
self::$_items[$type] = array(); |
|
109
|
2 |
|
$models = self::find() |
|
110
|
2 |
|
->where([ |
|
111
|
2 |
|
'type' => $type, |
|
112
|
2 |
|
'active' => 1, |
|
113
|
2 |
|
]) |
|
114
|
2 |
|
->orderBy('order') |
|
115
|
2 |
|
->all(); |
|
116
|
|
|
|
|
117
|
2 |
|
foreach ($models as $model) { |
|
118
|
2 |
|
self::$_items[$type][$model->code] = $model->name; |
|
119
|
2 |
|
} |
|
120
|
2 |
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|