SectionField::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;
7
use yii\db\ActiveQuery;
8
9
/**
10
 * Model class for table `{{%formgenerator_form_section_field}}`
11
 *
12
 * @property int $section_id
13
 * @property int $field_id
14
 * @property int $position
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 Section $section
22
 * @property Field $field
23
 * @property SolicitudeValue $solicitudeValues
24
 * @property array $solicitudeValuesData
25
 * @property array $solicitudeValuesDataDetail
26
 */
27
class SectionField extends \roaresearch\yii2\rmdb\models\Entity
28
{
29
    /**
30
     * @var string full class name of the model used in the relation
31
     * `getSection()`.
32
     */
33
    protected $sectionClass = Section::class;
34
35
    /**
36
     * @var string full class name of the model used in the relation
37
     * `getField()`.
38
     */
39
    protected $fieldClass = Field::class;
40
41
    /**
42
     * @var string full class name of the model used in the relation
43
     * `getSolicitudeValues()`.
44
     */
45
    protected $solicitudeValueClass = SolicitudeValue::class;
46
47
    /**
48
     * @inheritdoc
49
     */
50 10
    public static function tableName()
51
    {
52 10
        return '{{%formgenerator_form_section_field}}';
53
    }
54
55
    /**
56
     * @inheritdoc
57
     */
58 5
    protected function attributeTypecast(): array
59
    {
60 5
        return parent::attributeTypecast() + [
61 5
            'section_id' => 'integer',
62
            'field_id' => 'integer',
63
        ];
64
    }
65
66
    /**
67
     * @inheritdoc
68
     */
69 2
    public function rules()
70
    {
71
        return [
72 2
            [['section_id', 'field_id'], 'required'],
73
            [['section_id', 'field_id', 'position'], 'integer'],
74
            [
75
                ['section_id'],
76
                'exist',
77
                'skipOnError' => true,
78
                'targetClass' => Section::class,
79
                'targetAttribute' => ['section_id' => 'id'],
80
            ],
81
            [
82
                ['field_id'],
83
                'exist',
84
                'skipOnError' => true,
85
                'targetClass' => Field::class,
86
                'targetAttribute' => ['field_id' => 'id'],
87
                'message' => 'The field does not exists.',
88
            ],
89
            [['label'], 'trim'],
90
            [['label'], 'string'],
91
            [
92
                ['field_id'],
93
                'unique',
94
                'targetAttribute' => ['section_id', 'field_id'],
95
                'message' => 'Field already associated to the section.',
96
            ],
97
        ];
98
    }
99
100
    /**
101
     * @inheritdoc
102
     */
103 3
    public function transactions()
104
    {
105
        return [
106 3
            self::SCENARIO_DEFAULT => self::OP_ALL,
107
        ];
108
    }
109
110
    /**
111
     * @inheritdoc
112
     */
113 5
    public function behaviors()
114
    {
115 5
        return parent::behaviors() + [
116 5
            'position' => [
117
                'class' => Positionable::class,
118
                'parentAttribute' => 'section_id',
119
            ]
120
        ];
121
    }
122
123
    /**
124
     * @inheritdoc
125
     */
126 2
    public function attributeLabels()
127
    {
128 2
        return array_merge([
129 2
            'section_id' => 'Section ID',
130
            'field_id' => 'Field ID',
131
            'label' => 'label',
132 2
        ], parent::attributeLabels());
133
    }
134
135
    /**
136
     * @return ActiveQuery
137
     */
138 5
    public function getSection(): ActiveQuery
139
    {
140 5
        return $this->hasOne($this->sectionClass, ['id' => 'section_id']);
141
    }
142
143
    /**
144
     * @return ActiveQuery
145
     */
146 4
    public function getField(): ActiveQuery
147
    {
148 4
        return $this->hasOne($this->fieldClass, ['id' => 'field_id']);
149
    }
150
151
    /**
152
     * @return ActiveQuery
153
     */
154 1
    public function getSolicitudeValues(): ActiveQuery
155
    {
156 1
        return $this->hasMany($this->solicitudeValueClass, [
157 1
            'field_id' => 'field_id',
158
            'section_id' => 'section_id',
159 1
        ])->inverseOf('sectionField');
160
    }
161
162
    /**
163
     * @return ActiveQuery
164
     */
165 1
    public function getSolicitudeValuesDetail(): ActiveQuery
166
    {
167 1
        return Yii::configure(
168 1
            $this->getSolicitudeValues(),
169 1
            ['multiple' => false]
170
        )
171 1
            ->select([
172 1
                'count' => 'count(value)',
173
                'countDistinct' => 'count(distinct value)',
174
            ])
175 1
            ->groupBy(['section_id', 'field_id'])
176 1
            ->inverseOf(null)
177 1
            ->asArray();
178
    }
179
}
180