|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* _ __ __ _____ _____ ___ ____ _____ |
|
5
|
|
|
* | | / // // ___//_ _// || __||_ _| |
|
6
|
|
|
* | |/ // /(__ ) / / / /| || | | | |
|
7
|
|
|
* |___//_//____/ /_/ /_/ |_||_| |_| |
|
8
|
|
|
* @link http://vistart.name/ |
|
9
|
|
|
* @copyright Copyright (c) 2016 vistart |
|
10
|
|
|
* @license http://vistart.name/license/ |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace vistart\Models\traits; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* 使用此 trait 的类必须与使用 UserRelationTrait 的类配合使用。 |
|
17
|
|
|
* $contentAttribute 关系组名称。 |
|
18
|
|
|
* $contentTypeAttribute 关系组类型。 |
|
19
|
|
|
* @version 2.0 |
|
20
|
|
|
* @author vistart <[email protected]> |
|
21
|
|
|
*/ |
|
22
|
|
|
trait UserRelationGroupTrait |
|
23
|
|
|
{ |
|
24
|
|
|
|
|
25
|
|
|
public $relationClass; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Attach events associated with user relation group. |
|
29
|
|
|
*/ |
|
30
|
2 |
|
public function initUserRelationGroupEvents() |
|
31
|
|
|
{ |
|
32
|
2 |
|
$this->on(static::EVENT_BEFORE_DELETE, [$this, 'onDeleteGroup']); |
|
|
|
|
|
|
33
|
2 |
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* the event triggered before deleting group. |
|
37
|
|
|
* I do not remove group's guid from groupsAttribute which contains the guid |
|
38
|
|
|
* of group to be deleted. |
|
39
|
|
|
* @param \yii\base\Event $event |
|
40
|
|
|
*/ |
|
41
|
1 |
|
public function onDeleteGroup($event) |
|
|
|
|
|
|
42
|
|
|
{ |
|
43
|
|
|
/* |
|
44
|
|
|
$relationClass = $this->relationClass; |
|
45
|
|
|
if (!is_string($relationClass)) { |
|
46
|
|
|
throw new \yii\base\NotSupportedException('You must specify the name of relation class.'); |
|
47
|
|
|
} |
|
48
|
|
|
$sender = $event->sender; |
|
49
|
|
|
$groupGuid = $sender->guid; |
|
50
|
|
|
$createdByAttribute = $sender->createdByAttribute; |
|
51
|
|
|
$relations = $relationClass::findOnesAllRelations($sender->$createdByAttribute); |
|
52
|
|
|
foreach ($relations as $relation) { |
|
53
|
|
|
$relation->removeGroup($groupGuid); |
|
54
|
|
|
if (!$relation->save() && (YII_ENV !== YII_ENV_PROD || YII_DEBUG)) { |
|
55
|
|
|
$sender->recordWarnings(); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
*/ |
|
59
|
1 |
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
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
Idableprovides a methodequalsIdthat 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.