-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathManyToMany.php
81 lines (54 loc) · 1.8 KB
/
ManyToMany.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
<?php
/**
* Created by PhpStorm.
* User: joels
* Date: 28/02/2017
* Time: 10:49 AM
*/
namespace enigmatix\select2;
use yii\base\InvalidConfigException;
use yii\db\ActiveQuery;
use yii\helpers\ArrayHelper;
use yii\bootstrap\Html;
use yii\helpers\Inflector;
class ManyToMany extends Relate
{
public function init() {
if($this->list == null){
$func = 'get'. Inflector::camelize($this->name);
$valuelist = call_user_func([$this->model, $func]);
if(!$valuelist instanceof ActiveQuery)
throw new InvalidConfigException('An instance of ' . ActiveQuery::className() . ' must be supplied to the '
. ' value parameter');
$list = $valuelist->all();
$listValues = [];
foreach ($list as $model)
$listValues[$model->id] = $model->name;
$this->list = $listValues;
$this->value = count($listValues) ? array_keys($listValues) : [];
}
$this->fieldModel = new $valuelist->modelClass;
parent::init();
}
protected function getFieldOptions() {
return ArrayHelper::merge(parent::getFieldOptions(), ['multiple' => true]);
}
protected function renderLabel(){
if($this->label !== null){
return Html::label($this->label, $this->id);
}
$fieldName = $this->getFieldName();
return Html::label(Inflector::titleize($fieldName), $this->id);
}
protected function renderfield() {
return $this->renderLabel() . Html::dropDownList(
$this->name,
$this->value,
$this->list,
$this->getFieldOptions()
);
}
public function getFieldName(){
return Html::getAttributeName($this->name);
}
}