|
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\traits; |
|
14
|
|
|
|
|
15
|
|
|
use rhosocial\base\models\models\BaseUserModel; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* This trait is used for building query class which contains mutual relation operations. |
|
19
|
|
|
* |
|
20
|
|
|
* @version 1.0 |
|
21
|
|
|
* @author vistart <[email protected]> |
|
22
|
|
|
*/ |
|
23
|
|
|
trait MutualQueryTrait |
|
24
|
|
|
{ |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Get the opposite relation. |
|
28
|
|
|
* @param BaseUserModel|string $user initiator |
|
29
|
|
|
* @param BaseUserModel|string $other recipient. |
|
30
|
|
|
* @param Connection $database |
|
31
|
|
|
* @return |
|
32
|
|
|
*/ |
|
33
|
1 |
|
public function opposite($user, $other, $database = null) |
|
34
|
|
|
{ |
|
35
|
1 |
|
$model = $this->noInitModel; |
|
|
|
|
|
|
36
|
1 |
|
return $this->andWhere([$model->createdByAttribute => BaseUserModel::compositeGUIDs($other), $model->otherGuidAttribute => BaseUserModel::compositeGUIDs($user)])->one($database); |
|
|
|
|
|
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Get all the opposites. |
|
41
|
|
|
* @param string $user initator. |
|
42
|
|
|
* @param array $others all recipients. |
|
43
|
|
|
* @param Connection $database |
|
44
|
|
|
* @return array instances. |
|
45
|
|
|
*/ |
|
46
|
1 |
|
public function opposites($user, $others = [], $database = null) |
|
47
|
|
|
{ |
|
48
|
1 |
|
$model = $this->noInitModel; |
|
49
|
1 |
|
if ($user instanceof BaseUserModel) { |
|
50
|
1 |
|
$user = $user->getGUID(); |
|
51
|
|
|
} |
|
52
|
1 |
|
$query = $this->andWhere([$model->otherGuidAttribute => $user]); |
|
|
|
|
|
|
53
|
1 |
|
if (!empty($others)) { |
|
54
|
1 |
|
$query = $query->andWhere([$model->createdByAttribute => BaseUserModel::compositeGUIDs($others)]); |
|
55
|
|
|
} |
|
56
|
1 |
|
return $query->all($database); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Specify initiators. |
|
61
|
|
|
* @param string|array $users the guid of initiator if string, or guid array |
|
62
|
|
|
* of initiators if array. |
|
63
|
|
|
* @return \static $this |
|
64
|
|
|
*/ |
|
65
|
19 |
|
public function initiators($users = []) |
|
66
|
|
|
{ |
|
67
|
19 |
|
if (empty($users)) { |
|
68
|
1 |
|
return $this; |
|
69
|
|
|
} |
|
70
|
19 |
|
$model = $this->noInitModel; |
|
71
|
19 |
|
return $this->andWhere([$model->createdByAttribute => BaseUserModel::compositeGUIDs($users)]); |
|
|
|
|
|
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Specify recipients. |
|
76
|
|
|
* @param string|array $users the guid of recipient if string, or guid array |
|
77
|
|
|
* of recipients if array. |
|
78
|
|
|
* @return \static $this |
|
79
|
|
|
*/ |
|
80
|
19 |
|
public function recipients($users = []) |
|
81
|
|
|
{ |
|
82
|
19 |
|
if (empty($users)) { |
|
83
|
1 |
|
return $this; |
|
84
|
|
|
} |
|
85
|
19 |
|
$model = $this->noInitModel; |
|
86
|
19 |
|
return $this->andWhere([$model->otherGuidAttribute => BaseUserModel::compositeGUIDs($users)]); |
|
|
|
|
|
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: