Passed
Push — master ( 30d0f9...a8ef92 )
by Andrey
07:18
created

Social::scenarios()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace app\models;
4
5
use Itstructure\AdminModule\interfaces\ModelInterface;
6
7
/**
8
 * This is the model class for table "social".
9
 *
10
 * @property int $id
11
 * @property string $icon
12
 * @property string $url
13
 * @property string $created_at
14
 * @property string $updated_at
15
 *
16
 * @property ContactSocial[] $contactSocial
17
 * @property Contact[] $contacts
18
 *
19
 * @package app\models
20
 */
21
class Social extends ActiveRecord implements ModelInterface
22
{
23
    /**
24
     * {@inheritdoc}
25
     */
26
    public static function tableName()
27
    {
28
        return 'social';
29
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34
    public function rules()
35
    {
36
        return [
37
            [
38
                [
39
                    'icon',
40
                    'url',
41
                    'contacts',
42
                ],
43
                'required'
44
            ],
45
            [
46
                [
47
                    'created_at',
48
                    'updated_at'
49
                ],
50
                'safe'
51
            ],
52
            [
53
                [
54
                    'icon'
55
                ],
56
                'string',
57
                'max' => 64
58
            ],
59
            [
60
                [
61
                    'url'
62
                ],
63
                'string',
64
                'max' => 255
65
            ],
66
        ];
67
    }
68
69
    /**
70
     * List if attributes.
71
     *
72
     * @return array
73
     */
74
    public function attributes()
75
    {
76
        return [
77
            'id',
78
            'icon',
79
            'url',
80
            'created_at',
81
            'updated_at',
82
        ];
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88
    public function attributeLabels()
89
    {
90
        return [
91
            'id' => 'ID',
92
            'icon' => 'Icon',
93
            'url' => 'Url',
94
            'contacts' => 'Contacts',
95
            'created_at' => 'Created At',
96
            'updated_at' => 'Updated At',
97
        ];
98
    }
99
100
    /**
101
     * @return array
102
     */
103
    public function scenarios()
104
    {
105
        $scenarios = parent::scenarios();
106
107
        $scenarios[self::SCENARIO_CREATE][] = 'contacts';
108
        $scenarios[self::SCENARIO_UPDATE][] = 'contacts';
109
110
        return $scenarios;
111
    }
112
113
    /**
114
     * @return \yii\db\ActiveQuery
115
     */
116
    public function getContactSocial()
117
    {
118
        return $this->hasMany(ContactSocial::class, [
119
            'social_id' => 'id'
120
        ]);
121
    }
122
123
    /**
124
     * @return \yii\db\ActiveQuery
125
     */
126
    public function getContacts()
127
    {
128
        return $this->hasMany(Contact::class, [
129
            'id' => 'contacts_id'
130
        ])->viaTable('contacts_social', [
131
            'social_id' => 'id'
132
        ]);
133
    }
134
135
    /**
136
     * @param $contacts
137
     *
138
     * @return void
139
     */
140
    public function setContacts($contacts): void
141
    {
142
        $this->contacts = $contacts;
143
    }
144
145
    /**
146
     * Link with contacts entity after save.
147
     *
148
     * @param bool $insert
149
     *
150
     * @param array $changedAttributes
151
     */
152
    public function afterSave($insert, $changedAttributes)
153
    {
154
        $this->linkWithContacts(empty($this->contacts) ? [] : $this->contacts);
155
156
        parent::afterSave($insert, $changedAttributes);
157
    }
158
159
    /**
160
     * Returns id of the model.
161
     *
162
     * @return int
163
     */
164
    public function getId()
165
    {
166
        return $this->id;
167
    }
168
169
    /**
170
     * Link with contacts entity.
171
     *
172
     * @param array $contactList
173
     */
174
    private function linkWithContacts(array $contactList): void
175
    {
176
        ContactSocial::deleteAll([
177
            'social_id' => $this->id
178
        ]);
179
180
        foreach ($contactList as $contactId) {
181
            $contactSocial = new ContactSocial();
182
            $contactSocial->social_id = $this->id;
183
            $contactSocial->contacts_id = $contactId;
184
            $contactSocial->save();
185
        }
186
    }
187
}
188