Section::tableName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace roaresearch\yii2\formgenerator\models;
4
5
use roaresearch\yii2\formgenerator\behaviors\Positionable;
6
use yii\db\ActiveQuery;
7
8
/**
9
 * Model class for table `{{%formgenerator_form_section}}`
10
 *
11
 * @property int $id
12
 * @property int $form_id
13
 * @property int $position
14
 * @property string $name
15
 * @property string $label
16
 * @property int $created_by
17
 * @property string $created_at
18
 * @property int $updated_by
19
 * @property string $updated_at
20
 *
21
 * @property Form $form
22
 * @property SectionField[] $sectionFields
23
 * @property Field[] $fieds
24
 */
25
class Section extends \roaresearch\yii2\rmdb\models\Entity
26
{
27
    /**
28
     * @var string full class name of the model used in the relation
29
     * `getForm()`.
30
     */
31
    protected $formClass = Form::class;
32
33
    /**
34
     * @var string full class name of the model used in the relation
35
     * `getSectionFields()`.
36
     */
37
    protected $sectionFieldClass = SectionField::class;
38
39
    /**
40
     * @var string full class name of the model used in the relation
41
     * `getFields()`.
42
     */
43
    protected $fieldClass = Field::class;
44
45
    /**
46
     * @inheritdoc
47
     */
48 20
    public static function tableName()
49
    {
50 20
        return '{{%formgenerator_form_section}}';
51
    }
52
53
    /**
54
     * @inheritdoc
55
     */
56 15
    protected function attributeTypecast(): array
57
    {
58 15
        return parent::attributeTypecast() + [
59 15
            'id' => 'integer',
60
            'form_id' => 'integer',
61
        ];
62
    }
63
64
    /**
65
     * @inheritdoc
66
     */
67 3
    public function transactions()
68
    {
69
        return [
70 3
            self::SCENARIO_DEFAULT => self::OP_ALL,
71
        ];
72
    }
73
74
    /**
75
     * @inheritdoc
76
     */
77 15
    public function behaviors()
78
    {
79 15
        return parent::behaviors() + [
80 15
            'position' => [
81
                'class' => Positionable::class,
82
                'parentAttribute' => 'form_id',
83
            ]
84
        ];
85
    }
86
87
    /**
88
     * @inheritdoc
89
     */
90 2
    public function rules()
91
    {
92
        return [
93 2
            [['form_id', 'name', 'label'], 'required'],
94
            [['form_id', 'position'], 'integer'],
95
            [
96
                ['form_id'],
97
                'exist',
98
                'skipOnError' => true,
99
                'targetClass' => Form::class,
100
                'targetAttribute' => ['form_id' => 'id'],
101
            ],
102
            [['name', 'label'], 'trim'],
103
            [['name', 'label'], 'string', 'min' => 6],
104
            [
105
                ['name'],
106
                'unique',
107
                'targetAttribute' => ['form_id', 'name'],
108
                'message' => 'Section name "{value}" already in use '
109
                    . 'for this form.',
110
            ],
111
        ];
112
    }
113
114
    /**
115
     * @inheritdoc
116
     */
117 3
    public function attributeLabels()
118
    {
119 3
        return array_merge([
120 3
            'id' => 'ID',
121
            'form_id' => 'Form ID',
122
            'name' => 'Section name',
123
            'label' => 'Section label',
124 3
        ], parent::attributeLabels());
125
    }
126
127
    /**
128
     * @return ActiveQuery
129
     */
130 15
    public function getForm(): ActiveQuery
131
    {
132 15
        return $this->hasOne($this->formClass, ['id' => 'form_id']);
133
    }
134
135
    /**
136
     * @return ActiveQuery
137
     */
138
    public function getSectionFields(): ActiveQuery
139
    {
140
        return $this->hasMany($this->sectionFieldClass, ['section_id' => 'id'])
141
            ->inverseOf('section');
142
    }
143
144
    /**
145
     * @return ActiveQuery sibling stages for the same workflow
146
     */
147
    public function getFields(): ActiveQuery
148
    {
149
        return $this->hasMany($this->fieldClass, ['id' => 'field_id'])
150
            ->via('sectionFields');
151
    }
152
}
153