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
|
|
|
/** |
16
|
|
|
* Description of MutualQueryTrait |
17
|
|
|
* |
18
|
|
|
* @author vistart <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
trait MutualQueryTrait |
21
|
|
|
{ |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Get the opposite relation. |
25
|
|
|
* @param BaseUserModel|string $user initiator |
26
|
|
|
* @param BaseUserModel|string $other recipient. |
27
|
|
|
* @param Connection $database |
28
|
|
|
* @return |
29
|
|
|
*/ |
30
|
8 |
|
public function opposite($user, $other, $database = null) |
31
|
|
|
{ |
32
|
8 |
|
$model = $this->noInitModel; |
|
|
|
|
33
|
8 |
|
return $this->andWhere([$model->createdByAttribute => $other, $model->otherGuidAttribute => $user])->one($database); |
|
|
|
|
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Get all the opposites. |
38
|
|
|
* @param string $user initator. |
39
|
|
|
* @param array $others all recipients. |
40
|
|
|
* @param Connection $database |
41
|
|
|
* @return array instances. |
42
|
|
|
*/ |
43
|
7 |
|
public function opposites($user, $others = [], $database = null) |
44
|
|
|
{ |
45
|
7 |
|
$model = $this->noInitModel; |
46
|
7 |
|
$query = $this->andWhere([$model->otherGuidAttribute => $user]); |
|
|
|
|
47
|
7 |
|
if (!empty($others)) { |
48
|
|
|
$query = $query->andWhere([$model->createdByAttribute => array_values($others)]); |
49
|
|
|
} |
50
|
7 |
|
return $query->all($database); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Specify initiators. |
55
|
|
|
* @param string|array $users the guid of initiator if string, or guid array |
56
|
|
|
* of initiators if array. |
57
|
|
|
* @return \static $this |
58
|
|
|
*/ |
59
|
8 |
|
public function initiators($users = []) |
60
|
|
|
{ |
61
|
8 |
|
if (empty($users)) { |
62
|
|
|
return $this; |
63
|
|
|
} |
64
|
8 |
|
$model = $this->noInitModel; |
65
|
8 |
|
return $this->andWhere([$model->createdByAttribute => $users]); |
|
|
|
|
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Specify recipients. |
70
|
|
|
* @param string|array $users the guid of recipient if string, or guid array |
71
|
|
|
* of recipients if array. |
72
|
|
|
* @return \static $this |
73
|
|
|
*/ |
74
|
10 |
|
public function recipients($users = []) |
75
|
|
|
{ |
76
|
10 |
|
if (empty($users)) { |
77
|
|
|
return $this; |
78
|
|
|
} |
79
|
10 |
|
$model = $this->noInitModel; |
80
|
10 |
|
return $this->andWhere([$model->otherGuidAttribute => $users]); |
|
|
|
|
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
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: