|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* HiPanel core package. |
|
4
|
|
|
* |
|
5
|
|
|
* @link https://hipanel.com/ |
|
6
|
|
|
* @package hipanel-core |
|
7
|
|
|
* @license BSD-3-Clause |
|
8
|
|
|
* @copyright Copyright (c) 2014-2017, HiQDev (http://hiqdev.com/) |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace hipanel\models; |
|
12
|
|
|
|
|
13
|
|
|
use Yii; |
|
14
|
|
|
use yii\helpers\ArrayHelper; |
|
15
|
|
|
|
|
16
|
|
|
class Ref extends \hiqdev\hiart\ActiveRecord |
|
17
|
|
|
{ |
|
18
|
|
|
use \hipanel\base\ModelTrait; |
|
19
|
|
|
|
|
20
|
|
|
public function rules() |
|
21
|
|
|
{ |
|
22
|
|
|
return [ |
|
23
|
|
|
[['id', 'no'], 'integer'], |
|
24
|
|
|
[['name', 'oname', 'label'], 'safe'], |
|
25
|
|
|
]; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Finds models by specified $name and $options parameters, |
|
30
|
|
|
* transforms them to key-value structure. |
|
31
|
|
|
* Name: attribute `name` |
|
32
|
|
|
* Value: attribute `label`. |
|
33
|
|
|
* You can change name of returning attributes `name` and `label` |
|
34
|
|
|
* You should define `mapOptions` in `options` array |
|
35
|
|
|
* and define keys `from` for Name and `to` for Value. |
|
36
|
|
|
* |
|
37
|
|
|
* @param string $name |
|
38
|
|
|
* @param string|false $translate |
|
39
|
|
|
* @param array $options |
|
40
|
|
|
* @return array |
|
41
|
|
|
*/ |
|
42
|
|
|
public static function getList($name, $translate = null, $options = []) |
|
43
|
|
|
{ |
|
44
|
|
|
$mapOptions = ArrayHelper::remove($options, 'mapOptions'); |
|
45
|
|
|
$models = static::findCached($name, $translate, $options); |
|
46
|
|
|
|
|
47
|
|
|
$from = ArrayHelper::remove($mapOptions, 'from', 'name'); |
|
48
|
|
|
$to = ArrayHelper::remove($mapOptions, 'to', 'label'); |
|
49
|
|
|
$group = ArrayHelper::remove($mapOptions, 'group', null); |
|
50
|
|
|
|
|
51
|
|
|
return ArrayHelper::map($models, $from, $to, $group); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public static function getListRecursively($name, $translate = null, $options = []) |
|
55
|
|
|
{ |
|
56
|
|
|
return self::getList($name, $translate, array_merge($options, ['with_recursive' => true])); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public static function findCached($name, $translate = null, $options = []) |
|
60
|
|
|
{ |
|
61
|
|
|
if ($translate === null) { |
|
62
|
|
|
$translate = 'hipanel'; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
$data = Yii::$app->get('cache')->getOrSet([__METHOD__, $name, $options], function () use ($name, $options) { |
|
66
|
|
|
$conditions = array_merge(['gtype' => $name], $options); |
|
67
|
|
|
$result = self::find()->where($conditions)->all(); |
|
68
|
|
|
|
|
69
|
|
|
return $result; |
|
70
|
|
|
}, 3600); |
|
71
|
|
|
|
|
72
|
|
|
return array_map(function ($model) use ($translate) { |
|
73
|
|
|
/** @var self $model */ |
|
74
|
|
|
if ($translate !== false) { |
|
75
|
|
|
$model->label = Yii::t($translate, $model->label); |
|
|
|
|
|
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
return $model; |
|
79
|
|
|
}, $data); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
Since your code implements the magic setter
_set, this function will be called for any write access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.