1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* _ __ __ _____ _____ ___ ____ _____ |
5
|
|
|
* | | / // // ___//_ _// || __||_ _| |
6
|
|
|
* | |/ // /(__ ) / / / /| || | | | |
7
|
|
|
* |___//_//____/ /_/ /_/ |_||_| |_| |
8
|
|
|
* @link https://vistart.me/ |
9
|
|
|
* @copyright Copyright (c) 2016 - 2017 vistart |
10
|
|
|
* @license https://vistart.me/license/ |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace rhosocial\base\models\models; |
14
|
|
|
|
15
|
|
|
use rhosocial\base\models\queries\BaseUserRelationQuery; |
16
|
|
|
use rhosocial\base\models\traits\UserRelationTrait; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* This abstract class helps you build user relation model. |
20
|
|
|
* If you want to use group feature, the BaseUserRelationGroup's extended class |
21
|
|
|
* must be used coordinately. |
22
|
|
|
* Basic usage: |
23
|
|
|
* - Implementation: |
24
|
|
|
* ~~~php |
25
|
|
|
* class SingleRelation extends BaseUserRelationModel { |
26
|
|
|
* public $relationType = 0; // 0 represents single relation, 1 represents mutual relation. 1 is default. |
27
|
|
|
* public $multiBlamesAttribute = 'groups'; // you should redeclare this property if needed. 'blames' is default. |
28
|
|
|
* |
29
|
|
|
* public function init() |
30
|
|
|
* { |
31
|
|
|
* $this->multiBlamesClass = UserRelationGroup::class; // if you need relation group feature, `$multiBlamesClass` property should be specified. |
32
|
|
|
* parent::init(); // parent's call should be placed at the end of init() method. |
33
|
|
|
* } |
34
|
|
|
* } |
35
|
|
|
* ~~~ |
36
|
|
|
* or: |
37
|
|
|
* ~~~php |
38
|
|
|
* class Relation extends BaseUserRelationModel { |
39
|
|
|
* public $multiBlamesAttribute = 'groups'; // you should redeclare this property if needed. 'blames' is default. |
40
|
|
|
* |
41
|
|
|
* public function init() |
42
|
|
|
* { |
43
|
|
|
* $this->multiBlamesClass = UserRelationGroup::class; // if you need relation group feature, `$multiBlamesClass` property should be specified. |
44
|
|
|
* parent::init(); // parent's call should be placed at the end of init() method. |
45
|
|
|
* } |
46
|
|
|
* } |
47
|
|
|
* ~~~ |
48
|
|
|
* Then you can implememt your own `tableName()` for the name of specified table, |
49
|
|
|
* and `find()` for convenient of IDE. |
50
|
|
|
* - Instantiation: |
51
|
|
|
* ~~~php |
52
|
|
|
* $relation = $user->findOneOrCreate(SingleRelation, ['other_guid' => $other->guid]); |
53
|
|
|
* ~~~ |
54
|
|
|
* The above is same as the following: |
55
|
|
|
* ~~~php |
56
|
|
|
* $relation = SingleRelation::buildRelation($user, $other); |
57
|
|
|
* ~~~ |
58
|
|
|
* @version 1.0 |
59
|
|
|
* @author vistart <[email protected]> |
60
|
|
|
*/ |
61
|
|
|
abstract class BaseUserRelationModel extends BaseBlameableModel |
62
|
|
|
{ |
63
|
|
|
use UserRelationTrait; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @var false|string this model will not need to be confirmed any more. |
67
|
|
|
* If you consider it required, please redeclare it by yourself. |
68
|
|
|
*/ |
69
|
|
|
public $confirmationAttribute = false; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @var false|string this model will not record content any more. |
73
|
|
|
* If you consider it required, please redeclare it by yourself. |
74
|
|
|
*/ |
75
|
|
|
public $contentAttribute = false; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @var false|string this model will not record updater when being updated. |
79
|
|
|
* If you consider it required, please redeclare it by yourself. |
80
|
|
|
*/ |
81
|
|
|
public $updatedByAttribute = false; |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* This method will assign the `$queryClass` property if it is not string. |
85
|
|
|
*/ |
86
|
16 |
|
public function init() |
87
|
|
|
{ |
88
|
16 |
|
if (!is_string($this->queryClass)) { |
89
|
16 |
|
$this->queryClass = BaseUserRelationQuery::class; |
90
|
|
|
} |
91
|
16 |
|
if ($this->skipInit) { |
92
|
16 |
|
return; |
93
|
|
|
} |
94
|
16 |
|
$this->initUserRelationEvents(); |
95
|
16 |
|
parent::init(); |
96
|
16 |
|
} |
97
|
|
|
} |
98
|
|
|
|