|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace roaresearch\yii2\formgenerator\models; |
|
4
|
|
|
|
|
5
|
|
|
use yii\{base\Model, db\ActiveQuery}; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Model class for table `{{%formgenerator_field}}` |
|
9
|
|
|
* |
|
10
|
|
|
* @property int $id |
|
11
|
|
|
* @property int $data_type_id |
|
12
|
|
|
* @property string $name |
|
13
|
|
|
* @property string $label |
|
14
|
|
|
* @property int $created_by |
|
15
|
|
|
* @property string $created_at |
|
16
|
|
|
* @property int $updated_by |
|
17
|
|
|
* @property string $updated_at |
|
18
|
|
|
* |
|
19
|
|
|
* @property DataType $dataType |
|
20
|
|
|
* @property Rules[] $rules |
|
21
|
|
|
*/ |
|
22
|
|
|
class Field extends \roaresearch\yii2\rmdb\models\Entity |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @var string full class name of the model used in the relation |
|
26
|
|
|
* `getDataType()`. |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $dataTypeClass = DataType::class; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var string full class name of the model used in the relation |
|
32
|
|
|
* `getRules()`. |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $ruleClass = FieldRule::class; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @inheritdoc |
|
38
|
|
|
*/ |
|
39
|
28 |
|
public static function tableName() |
|
40
|
|
|
{ |
|
41
|
28 |
|
return '{{%formgenerator_field}}'; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @inheritdoc |
|
46
|
|
|
*/ |
|
47
|
23 |
|
protected function attributeTypecast(): array |
|
48
|
|
|
{ |
|
49
|
23 |
|
return parent::attributeTypecast() + [ |
|
50
|
23 |
|
'id' => 'integer', |
|
51
|
|
|
'data_type_id' => 'integer', |
|
52
|
|
|
]; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @inheritdoc |
|
57
|
|
|
*/ |
|
58
|
2 |
|
public function rules() |
|
59
|
|
|
{ |
|
60
|
|
|
return [ |
|
61
|
2 |
|
[['data_type_id', 'name', 'label'], 'required'], |
|
62
|
|
|
[['data_type_id'], 'integer'], |
|
63
|
|
|
[ |
|
64
|
|
|
['data_type_id'], |
|
65
|
|
|
'exist', |
|
66
|
|
|
'skipOnError' => true, |
|
67
|
|
|
'targetClass' => DataType::class, |
|
68
|
|
|
'targetAttribute' => ['data_type_id' => 'id'], |
|
69
|
|
|
], |
|
70
|
|
|
[['name', 'label', 'service'], 'string', 'min' => 4], |
|
71
|
|
|
[['name'], 'unique'], |
|
72
|
|
|
]; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @inheritdoc |
|
77
|
|
|
*/ |
|
78
|
3 |
|
public function attributeLabels() |
|
79
|
|
|
{ |
|
80
|
3 |
|
return array_merge([ |
|
81
|
3 |
|
'id' => 'ID', |
|
82
|
|
|
'data_type_id' => 'Data Type ID', |
|
83
|
|
|
'name' => 'Field name', |
|
84
|
|
|
'label' => 'Field label', |
|
85
|
3 |
|
], parent::attributeLabels()); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @return ActiveQuery |
|
90
|
|
|
*/ |
|
91
|
6 |
|
public function getDataType(): ActiveQuery |
|
92
|
|
|
{ |
|
93
|
6 |
|
return $this->hasOne($this->dataTypeClass, ['id' => 'data_type_id']); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @return ActiveQuery |
|
98
|
|
|
*/ |
|
99
|
2 |
|
public function getRules(): ActiveQuery |
|
100
|
|
|
{ |
|
101
|
2 |
|
return $this->hasMany($this->ruleClass, ['field_id' => 'id']) |
|
102
|
2 |
|
->inverseOf('field'); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @return \yii\validators\Validator[] |
|
107
|
|
|
*/ |
|
108
|
2 |
|
public function buildValidators(Model $model, $attributes) |
|
109
|
|
|
{ |
|
110
|
2 |
|
$validators = []; |
|
111
|
|
|
|
|
112
|
2 |
|
foreach ($this->rules as $rule) { |
|
113
|
2 |
|
$validators[] = $rule->buildValidator($model, $attributes); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
2 |
|
return $validators; |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|