1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* _ __ __ _____ _____ ___ ____ _____ |
5
|
|
|
* | | / // // ___//_ _// || __||_ _| |
6
|
|
|
* | |/ // /(__ ) / / / /| || | | | |
7
|
|
|
* |___//_//____/ /_/ /_/ |_||_| |_| |
8
|
|
|
* @link https://vistart.name/ |
9
|
|
|
* @copyright Copyright (c) 2016 vistart |
10
|
|
|
* @license https://vistart.name/license/ |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace vistart\Models\traits; |
14
|
|
|
|
15
|
|
|
use vistart\Models\models\BaseUserModel; |
16
|
|
|
use vistart\Models\traits\MultipleBlameableTrait as mb; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Relation features. |
20
|
|
|
* This trait should be used in user relation model which is extended from |
21
|
|
|
* [[BaseBlameableModel]], and is specified `$userClass` property. And the user |
22
|
|
|
* class should be extended from [[BaseUserModel]], or any other classes used |
23
|
|
|
* [[UserTrait]]. |
24
|
|
|
* Notice: Several methods associated with "inserting", "updating" and "removing" may |
25
|
|
|
* involve more DB operations, I strongly recommend those methods to be placed in |
26
|
|
|
* transaction execution, in order to ensure data consistency. |
27
|
|
|
* If you want to use group feature, the class used [[UserRelationGroupTrait]] |
28
|
|
|
* must be used coordinately. |
29
|
|
|
* @property array $groupGuids the guid array of all groups which owned by current relation. |
30
|
|
|
* @property-read array $allGroups |
31
|
|
|
* @property-read array $nonGroupMembers |
32
|
|
|
* @property-read integer $groupsCount |
33
|
|
|
* @property-read array $groupsRules |
34
|
|
|
* @property boolean $isFavorite |
35
|
|
|
* @property-read \vistart\Models\models\BaseUserModel $initiator |
36
|
|
|
* @property-read \vistart\Models\models\BaseUserModel $recipient |
37
|
|
|
* @property-read \vistart\Models\models\BaseUserRelationModel $opposite |
38
|
|
|
* @version 2.0 |
39
|
|
|
* @author vistart <[email protected]> |
40
|
|
|
*/ |
41
|
|
|
trait UserRelationTrait |
42
|
|
|
{ |
43
|
|
|
use mb, |
44
|
|
|
MutualTrait { |
45
|
|
|
mb::addBlame as addGroup; |
46
|
|
|
mb::createBlame as createGroup; |
47
|
|
|
mb::addOrCreateBlame as addOrCreateGroup; |
48
|
|
|
mb::removeBlame as removeGroup; |
49
|
|
|
mb::removeAllBlames as removeAllGroups; |
50
|
|
|
mb::getBlame as getGroup; |
51
|
|
|
mb::getOrCreateBlame as getOrCreateGroup; |
52
|
|
|
mb::getBlameds as getGroupMembers; |
53
|
|
|
mb::getBlameGuids as getGroupGuids; |
54
|
|
|
mb::setBlameGuids as setGroupGuids; |
55
|
|
|
mb::getAllBlames as getAllGroups; |
56
|
|
|
mb::getNonBlameds as getNonGroupMembers; |
57
|
|
|
mb::getBlamesCount as getGroupsCount; |
58
|
|
|
mb::getMultipleBlameableAttributeRules as getGroupsRules; |
59
|
|
|
mb::getEmptyBlamesJson as getEmptyGroupJson; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @var string |
64
|
|
|
*/ |
65
|
|
|
public $remarkAttribute = 'remark'; |
66
|
|
|
public static $relationSingle = 0; |
67
|
|
|
public static $relationMutual = 1; |
68
|
|
|
public $relationType = 1; |
69
|
|
|
public $relationTypes = [ |
70
|
|
|
0 => 'Single', |
71
|
|
|
1 => 'Mutual', |
72
|
|
|
]; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @var string the attribute name of which determines the relation type. |
76
|
|
|
*/ |
77
|
|
|
public $mutualTypeAttribute = 'type'; |
78
|
|
|
public static $mutualTypeNormal = 0x00; |
79
|
|
|
public static $mutualTypeSuspend = 0x01; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @var array Mutual types. |
83
|
|
|
*/ |
84
|
|
|
public static $mutualTypes = [ |
85
|
|
|
0x00 => 'Normal', |
86
|
|
|
0x01 => 'Suspend', |
87
|
|
|
]; |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @var string the attribute name of which determines the `favorite` field. |
91
|
|
|
*/ |
92
|
|
|
public $favoriteAttribute = 'favorite'; |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Permit to build self relation. |
96
|
|
|
* @var boolean |
97
|
|
|
*/ |
98
|
|
|
public $relationSelf = false; |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Get whether this relation is favorite or not. |
102
|
|
|
* @return boolean |
103
|
|
|
*/ |
104
|
1 |
|
public function getIsFavorite() |
105
|
|
|
{ |
106
|
1 |
|
$favoriteAttribute = $this->favoriteAttribute; |
107
|
1 |
|
return is_string($favoriteAttribute) ? (int) $this->$favoriteAttribute > 0 : null; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Set favorite. |
112
|
|
|
* @param boolean $fav |
113
|
|
|
*/ |
114
|
1 |
|
public function setIsFavorite($fav) |
115
|
|
|
{ |
116
|
1 |
|
$favoriteAttribute = $this->favoriteAttribute; |
117
|
1 |
|
return is_string($favoriteAttribute) ? $this->$favoriteAttribute = ($fav ? 1 : 0) : null; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @inheritdoc |
122
|
|
|
*/ |
123
|
8 |
|
public function rules() |
124
|
|
|
{ |
125
|
8 |
|
return array_merge(parent::rules(), $this->getUserRelationRules()); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Validation rules associated with user relation. |
130
|
|
|
* @return array rules. |
131
|
|
|
*/ |
132
|
8 |
|
public function getUserRelationRules() |
133
|
|
|
{ |
134
|
8 |
|
$rules = []; |
135
|
8 |
|
if ($this->relationType == static::$relationMutual) { |
136
|
|
|
$rules = [ |
137
|
7 |
|
[[$this->mutualTypeAttribute], 'in', 'range' => array_keys(static::$mutualTypes)], |
138
|
7 |
|
[[$this->mutualTypeAttribute], 'default', 'value' => static::$mutualTypeNormal], |
139
|
7 |
|
]; |
140
|
7 |
|
} |
141
|
8 |
|
return array_merge($rules, $this->getRemarkRules(), $this->getFavoriteRules(), $this->getGroupsRules(), $this->getOtherGuidRules()); |
|
|
|
|
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Get remark. |
146
|
|
|
* @return string remark. |
147
|
|
|
*/ |
148
|
|
|
public function getRemark() |
149
|
|
|
{ |
150
|
|
|
$remarkAttribute = $this->remarkAttribute; |
151
|
|
|
return is_string($remarkAttribute) ? $this->$remarkAttribute : null; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Set remark. |
156
|
|
|
* @param string $remark |
157
|
|
|
* @return string remark. |
158
|
|
|
*/ |
159
|
|
|
public function setRemark($remark) |
160
|
|
|
{ |
161
|
|
|
$remarkAttribute = $this->remarkAttribute; |
162
|
|
|
return is_string($remarkAttribute) ? $this->$remarkAttribute = $remark : null; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Validation rules associated with remark attribute. |
167
|
|
|
* @return array rules. |
168
|
|
|
*/ |
169
|
8 |
|
public function getRemarkRules() |
170
|
|
|
{ |
171
|
8 |
|
return is_string($this->remarkAttribute) ? [ |
172
|
8 |
|
[[$this->remarkAttribute], 'string'], |
173
|
8 |
|
[[$this->remarkAttribute], 'default', 'value' => ''], |
174
|
8 |
|
] : []; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Validation rules associated with favorites attribute. |
179
|
|
|
* @return array rules. |
180
|
|
|
*/ |
181
|
8 |
|
public function getFavoriteRules() |
182
|
|
|
{ |
183
|
8 |
|
return is_string($this->favoriteAttribute) ? [ |
184
|
8 |
|
[[$this->favoriteAttribute], 'boolean'], |
185
|
8 |
|
[[$this->favoriteAttribute], 'default', 'value' => 0], |
186
|
8 |
|
] : []; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Validation rules associated with other guid attribute. |
191
|
|
|
* @return array rules. |
192
|
|
|
*/ |
193
|
8 |
|
public function getOtherGuidRules() |
194
|
|
|
{ |
195
|
8 |
|
$rules = array_merge($this->getMutualRules(), [ |
196
|
8 |
|
[[$this->otherGuidAttribute, $this->createdByAttribute], 'unique', 'targetAttribute' => [$this->otherGuidAttribute, $this->createdByAttribute]], |
|
|
|
|
197
|
8 |
|
]); |
198
|
8 |
|
return $rules; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Attach events associated with user relation. |
203
|
|
|
*/ |
204
|
8 |
|
public function initUserRelationEvents() |
205
|
|
|
{ |
206
|
8 |
|
$this->on(static::EVENT_INIT, [$this, 'onInitBlamesLimit']); |
|
|
|
|
207
|
8 |
|
$this->on(static::$eventNewRecordCreated, [$this, 'onInitGroups']); |
|
|
|
|
208
|
8 |
|
$this->on(static::$eventNewRecordCreated, [$this, 'onInitRemark']); |
|
|
|
|
209
|
8 |
|
$this->on(static::$eventMultipleBlamesChanged, [$this, 'onBlamesChanged']); |
|
|
|
|
210
|
8 |
|
$this->on(static::EVENT_AFTER_INSERT, [$this, 'onInsertRelation']); |
|
|
|
|
211
|
8 |
|
$this->on(static::EVENT_AFTER_UPDATE, [$this, 'onUpdateRelation']); |
|
|
|
|
212
|
8 |
|
$this->on(static::EVENT_AFTER_DELETE, [$this, 'onDeleteRelation']); |
|
|
|
|
213
|
8 |
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Get opposite relation against self. |
217
|
|
|
* @return \vistart\Models\models\BaseUserRelationModel |
218
|
|
|
*/ |
219
|
7 |
|
public function getOpposite() |
220
|
|
|
{ |
221
|
7 |
|
if ($this->isNewRecord) { |
|
|
|
|
222
|
1 |
|
return null; |
223
|
|
|
} |
224
|
7 |
|
$createdByAttribute = $this->createdByAttribute; |
225
|
7 |
|
$otherGuidAttribute = $this->otherGuidAttribute; |
226
|
7 |
|
return static::find()->opposite($this->$createdByAttribute, $this->$otherGuidAttribute); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Build new or return existed suspend mutual relation, of return null if |
231
|
|
|
* current type is not mutual. |
232
|
|
|
* @see buildRelation() |
233
|
|
|
* @param BaseUserModel|string $user Initiator or its GUID. |
234
|
|
|
* @param BaseUserModel|string $other Recipient or its GUID. |
235
|
|
|
* @return \vistart\Models\models\BaseUserRelationModel The relation will be |
236
|
|
|
* given if exists, or return a new relation. |
237
|
|
|
*/ |
238
|
1 |
|
public static function buildSuspendRelation($user, $other) |
239
|
|
|
{ |
240
|
1 |
|
$relation = static::buildRelation($user, $other); |
241
|
1 |
|
if ($relation->relationType != static::$relationMutual) { |
242
|
|
|
return null; |
243
|
|
|
} |
244
|
1 |
|
$btAttribute = $relation->mutualTypeAttribute; |
245
|
1 |
|
$relation->$btAttribute = static::$mutualTypeSuspend; |
246
|
1 |
|
return $relation; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* Build new or return existed normal relation. |
251
|
|
|
* The status of mutual relation will be changed to normal if it is not. |
252
|
|
|
* @see buildRelation() |
253
|
|
|
* @param BaseUserModel|string $user Initiator or its GUID. |
254
|
|
|
* @param BaseUserModel|string $other Recipient or its GUID. |
255
|
|
|
* @return \vistart\Models\models\BaseUserRelationModel The relation will be |
256
|
|
|
* given if exists, or return a new relation. |
257
|
|
|
*/ |
258
|
8 |
|
public static function buildNormalRelation($user, $other) |
259
|
|
|
{ |
260
|
8 |
|
$relation = static::buildRelation($user, $other); |
261
|
8 |
|
if ($relation->relationType == static::$relationMutual) { |
262
|
7 |
|
$btAttribute = $relation->mutualTypeAttribute; |
263
|
7 |
|
$relation->$btAttribute = static::$mutualTypeNormal; |
264
|
7 |
|
} |
265
|
8 |
|
return $relation; |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* Build new or return existed relation between initiator and recipient. |
270
|
|
|
* If relation between initiator and recipient is not found, new relation will |
271
|
|
|
* be built. If initiator and recipient are the same one and it is not allowed |
272
|
|
|
* to build self relation, null will be given. |
273
|
|
|
* If you want to know whether the relation exists, you can check the return |
274
|
|
|
* value of `getIsNewRecord()` method. |
275
|
|
|
* @param BaseUserModel|string $user Initiator or its GUID. |
276
|
|
|
* @param BaseUserModel|string $other Recipient or its GUID. |
277
|
|
|
* @return \vistart\Models\models\BaseUserRelationModel The relation will be |
278
|
|
|
* given if exists, or return a new relation. Or return null if not allowed |
279
|
|
|
* to build self relation, |
280
|
|
|
*/ |
281
|
8 |
|
protected static function buildRelation($user, $other) |
282
|
|
|
{ |
283
|
8 |
|
$relationQuery = static::find()->initiators($user)->recipients($other); |
284
|
8 |
|
$noInit = $relationQuery->noInitModel; |
285
|
8 |
|
$relation = $relationQuery->one(); |
286
|
8 |
|
if (!$relation) { |
287
|
8 |
|
$createdByAttribute = $noInit->createdByAttribute; |
288
|
8 |
|
$otherGuidAttribute = $noInit->otherGuidAttribute; |
289
|
8 |
|
$userClass = $noInit->userClass; |
290
|
8 |
|
if ($user instanceof BaseUserModel) { |
291
|
8 |
|
$userClass = $userClass ? : $user->className(); |
292
|
8 |
|
$user = $user->guid; |
293
|
8 |
|
} |
294
|
8 |
|
if ($other instanceof BaseUserModel) { |
295
|
8 |
|
$other = $other->guid; |
296
|
8 |
|
} |
297
|
8 |
|
if (!$noInit->relationSelf && $user == $other) { |
298
|
|
|
return null; |
299
|
|
|
} |
300
|
8 |
|
$relation = new static([$createdByAttribute => $user, $otherGuidAttribute => $other, 'userClass' => $userClass]); |
|
|
|
|
301
|
8 |
|
} |
302
|
8 |
|
return $relation; |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* Build opposite relation throughout the current relation. The opposite |
307
|
|
|
* relation will be given if existed. |
308
|
|
|
* @param \vistart\Models\models\BaseUserRelationModel $relation |
309
|
|
|
* @return \vistart\Models\models\BaseUserRelationModel |
310
|
|
|
*/ |
311
|
7 |
|
protected static function buildOppositeRelation($relation) |
312
|
|
|
{ |
313
|
7 |
|
if (!$relation) { |
314
|
|
|
return null; |
315
|
|
|
} |
316
|
7 |
|
$createdByAttribute = $relation->createdByAttribute; |
317
|
7 |
|
$otherGuidAttribute = $relation->otherGuidAttribute; |
318
|
7 |
|
$opposite = static::buildRelation($relation->$otherGuidAttribute, $relation->$createdByAttribute); |
319
|
7 |
|
if ($relation->relationType == static::$relationSingle) { |
320
|
|
|
$opposite->relationType = static::$relationSingle; |
321
|
7 |
|
} elseif ($relation->relationType == static::$relationMutual) { |
322
|
7 |
|
$mutualTypeAttribute = $relation->mutualTypeAttribute; |
323
|
7 |
|
$opposite->$mutualTypeAttribute = $relation->$mutualTypeAttribute; |
324
|
7 |
|
} |
325
|
7 |
|
return $opposite; |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
/** |
329
|
|
|
* Remove myself. |
330
|
|
|
* @return integer|false The number of relations removed, or false if the remove |
331
|
|
|
* is unsuccessful for some reason. Note that it is possible the number of relations |
332
|
|
|
* removed is 0, even though the remove execution is successful. |
333
|
|
|
*/ |
334
|
2 |
|
public function remove() |
335
|
|
|
{ |
336
|
2 |
|
return $this->delete(); |
|
|
|
|
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
/** |
340
|
|
|
* Remove first relation between initiator(s) and recipient(s). |
341
|
|
|
* @param BaseUserModel|string|array $user Initiator or its guid, or array of them. |
342
|
|
|
* @param BaseUserModel|string|array $other Recipient or its guid, or array of them. |
343
|
|
|
* @return integer|false The number of relations removed. |
344
|
|
|
*/ |
345
|
1 |
|
public static function removeOneRelation($user, $other) |
346
|
|
|
{ |
347
|
1 |
|
return static::find()->initiators($user)->recipients($other)->one()->delete(); |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
/** |
351
|
|
|
* Remove all relations between initiator(s) and recipient(s). |
352
|
|
|
* @param BaseUserModel|string|array $user Initiator or its guid, or array of them. |
353
|
|
|
* @param BaseUserModel|string|array $other Recipient or its guid, or array of them. |
354
|
|
|
* @return integer The number of relations removed. |
355
|
|
|
*/ |
356
|
2 |
|
public static function removeAllRelations($user, $other) |
357
|
|
|
{ |
358
|
2 |
|
$rni = static::buildNoInitModel(); |
359
|
2 |
|
$createdByAttribute = $rni->createdByAttribute; |
360
|
2 |
|
$otherGuidAttribute = $rni->otherGuidAttribute; |
361
|
2 |
|
return static::deleteAll([$createdByAttribute => $user, $otherGuidAttribute => $other]); |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
/** |
365
|
|
|
* Get first relation between initiator(s) and recipient(s). |
366
|
|
|
* @param BaseUserModel|string|array $user Initiator or its guid, or array of them. |
367
|
|
|
* @param BaseUserModel|string|array $other Recipient or its guid, or array of them. |
368
|
|
|
* @return \vistart\Models\models\BaseUserRelationModel |
369
|
|
|
*/ |
370
|
1 |
|
public static function findOneRelation($user, $other) |
371
|
|
|
{ |
372
|
1 |
|
return static::find()->initiators($user)->recipients($other)->one(); |
373
|
|
|
} |
374
|
|
|
|
375
|
|
|
/** |
376
|
|
|
* Get first opposite relation between initiator(s) and recipient(s). |
377
|
|
|
* @param BaseUserModel|string $user Initiator or its guid, or array of them. |
378
|
|
|
* @param BaseUserModel|string $other Recipient or its guid, or array of them. |
379
|
|
|
* @return \vistart\Models\models\BaseUserRelationModel |
380
|
|
|
*/ |
381
|
7 |
|
public static function findOneOppositeRelation($user, $other) |
382
|
|
|
{ |
383
|
7 |
|
return static::find()->initiators($other)->recipients($user)->one(); |
384
|
|
|
} |
385
|
|
|
|
386
|
|
|
/** |
387
|
|
|
* Get user's or users' all relations, or by specified groups. |
388
|
|
|
* @param BaseUserModel|string|array $user Initiator or its GUID, or Initiators or their GUIDs. |
389
|
|
|
* @param BaseUserRelationGroupModel|string|array|null $groups UserRelationGroup |
390
|
|
|
* or its guid, or array of them. If you do not want to delimit the groups, please assign null. |
391
|
|
|
* @return array all eligible relations |
392
|
|
|
*/ |
393
|
|
|
public static function findOnesAllRelations($user, $groups = null) |
394
|
|
|
{ |
395
|
|
|
return static::find()->initiators($user)->groups($groups)->all(); |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
/** |
399
|
|
|
* Initialize groups attribute. |
400
|
|
|
* @param \yii\base\Event $event |
401
|
|
|
*/ |
402
|
8 |
|
public function onInitGroups($event) |
403
|
|
|
{ |
404
|
8 |
|
$sender = $event->sender; |
405
|
8 |
|
$sender->removeAllGroups(); |
406
|
8 |
|
} |
407
|
|
|
|
408
|
|
|
/** |
409
|
|
|
* Initialize remark attribute. |
410
|
|
|
* @param \yii\base\Event $event |
411
|
|
|
*/ |
412
|
8 |
|
public function onInitRemark($event) |
413
|
|
|
{ |
414
|
8 |
|
$sender = $event->sender; |
415
|
8 |
|
$remarkAttribute = $sender->remarkAttribute; |
416
|
8 |
|
is_string($remarkAttribute) ? $sender->$remarkAttribute = '' : null; |
417
|
8 |
|
} |
418
|
|
|
|
419
|
|
|
/** |
420
|
|
|
* The event triggered after insert new relation. |
421
|
|
|
* The opposite relation should be inserted without triggering events |
422
|
|
|
* simultaneously after new relation inserted, |
423
|
|
|
* @param \yii\base\Event $event |
424
|
|
|
*/ |
425
|
8 |
|
public function onInsertRelation($event) |
426
|
|
|
{ |
427
|
8 |
|
$sender = $event->sender; |
428
|
8 |
|
if ($sender->relationType == static::$relationMutual) { |
429
|
7 |
|
$opposite = static::buildOppositeRelation($sender); |
430
|
7 |
|
$opposite->off(static::EVENT_AFTER_INSERT, [$opposite, 'onInsertRelation']); |
431
|
7 |
|
if (!$opposite->save()) { |
432
|
|
|
$opposite->recordWarnings(); |
433
|
|
|
} |
434
|
7 |
|
$opposite->on(static::EVENT_AFTER_INSERT, [$opposite, 'onInsertRelation']); |
435
|
7 |
|
} |
436
|
8 |
|
} |
437
|
|
|
|
438
|
|
|
/** |
439
|
|
|
* The event triggered after update relation. |
440
|
|
|
* The opposite relation should be updated without triggering events |
441
|
|
|
* simultaneously after existed relation removed. |
442
|
|
|
* @param \yii\base\Event $event |
443
|
|
|
*/ |
444
|
3 |
|
public function onUpdateRelation($event) |
445
|
|
|
{ |
446
|
3 |
|
$sender = $event->sender; |
447
|
3 |
|
if ($sender->relationType == static::$relationMutual) { |
448
|
3 |
|
$opposite = static::buildOppositeRelation($sender); |
449
|
3 |
|
$opposite->off(static::EVENT_AFTER_UPDATE, [$opposite, 'onUpdateRelation']); |
450
|
3 |
|
if (!$opposite->save()) { |
451
|
|
|
$opposite->recordWarnings(); |
452
|
|
|
} |
453
|
3 |
|
$opposite->on(static::EVENT_AFTER_UPDATE, [$opposite, 'onUpdateRelation']); |
454
|
3 |
|
} |
455
|
3 |
|
} |
456
|
|
|
|
457
|
|
|
/** |
458
|
|
|
* The event triggered after delete relation. |
459
|
|
|
* The opposite relation should be deleted without triggering events |
460
|
|
|
* simultaneously after existed relation removed. |
461
|
|
|
* @param \yii\base\Event $event |
462
|
|
|
*/ |
463
|
2 |
|
public function onDeleteRelation($event) |
464
|
|
|
{ |
465
|
2 |
|
$sender = $event->sender; |
466
|
2 |
|
if ($sender->relationType == static::$relationMutual) { |
467
|
2 |
|
$createdByAttribute = $sender->createdByAttribute; |
468
|
2 |
|
$otherGuidAttribute = $sender->otherGuidAttribute; |
469
|
2 |
|
$sender->off(static::EVENT_AFTER_DELETE, [$sender, 'onDeleteRelation']); |
470
|
2 |
|
static::removeAllRelations($sender->$otherGuidAttribute, $sender->$createdByAttribute); |
471
|
2 |
|
$sender->on(static::EVENT_AFTER_DELETE, [$sender, 'onDeleteRelation']); |
472
|
2 |
|
} |
473
|
2 |
|
} |
474
|
|
|
} |
475
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.