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\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 => $other, $model->otherGuidAttribute => $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
|
|
|
public function opposites($user, $others = [], $database = null) |
47
|
|
|
{ |
48
|
|
|
$model = $this->noInitModel; |
49
|
|
|
$query = $this->andWhere([$model->otherGuidAttribute => $user]); |
|
|
|
|
50
|
|
|
if (!empty($others)) { |
51
|
|
|
$query = $query->andWhere([$model->createdByAttribute => array_values($others)]); |
52
|
|
|
} |
53
|
|
|
return $query->all($database); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Specify initiators. |
58
|
|
|
* @param string|array $users the guid of initiator if string, or guid array |
59
|
|
|
* of initiators if array. |
60
|
|
|
* @return \static $this |
61
|
|
|
*/ |
62
|
16 |
|
public function initiators($users = []) |
63
|
|
|
{ |
64
|
16 |
|
if (empty($users)) { |
65
|
|
|
return $this; |
66
|
|
|
} |
67
|
16 |
|
$model = $this->noInitModel; |
68
|
16 |
|
return $this->andWhere([$model->createdByAttribute => $users]); |
|
|
|
|
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Specify recipients. |
73
|
|
|
* @param string|array $users the guid of recipient if string, or guid array |
74
|
|
|
* of recipients if array. |
75
|
|
|
* @return \static $this |
76
|
|
|
*/ |
77
|
16 |
|
public function recipients($users = []) |
78
|
|
|
{ |
79
|
16 |
|
if (empty($users)) { |
80
|
|
|
return $this; |
81
|
|
|
} |
82
|
16 |
|
$model = $this->noInitModel; |
83
|
16 |
|
return $this->andWhere([$model->otherGuidAttribute => $users]); |
|
|
|
|
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
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: