1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace SimpleCrud\Query\Traits; |
5
|
|
|
|
6
|
|
|
use InvalidArgumentException; |
7
|
|
|
use RuntimeException; |
8
|
|
|
use SimpleCrud\Row; |
9
|
|
|
use SimpleCrud\RowCollection; |
10
|
|
|
use SimpleCrud\Table; |
11
|
|
|
|
12
|
|
|
trait HasRelatedWith |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @param Row|RowCollection|Table |
16
|
|
|
* @param mixed $related |
17
|
|
|
*/ |
18
|
|
|
public function relatedWith($related): self |
19
|
|
|
{ |
20
|
|
|
if ($related instanceof Row || $related instanceof RowCollection) { |
21
|
|
|
return $this->applyRowRelation($related); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
if ($related instanceof Table) { |
25
|
|
|
return $this->applyTableRelation($related); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
throw new InvalidArgumentException( |
29
|
|
|
'Invalid argument type. Only instances of Row, Table and RowCollection are allowed' |
30
|
|
|
); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
private function applyTableRelation(Table $table2): self |
34
|
|
|
{ |
35
|
|
|
$table1 = $this->table; |
|
|
|
|
36
|
|
|
|
37
|
|
|
//Has one |
38
|
|
|
if ($field = $table1->getJoinField($table2)) { |
39
|
|
|
return $this->where("{$field} IS NOT NULL"); |
|
|
|
|
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
//Has many |
43
|
|
View Code Duplication |
if ($field = $table2->getJoinField($table1)) { |
|
|
|
|
44
|
|
|
$this->query |
|
|
|
|
45
|
|
|
->join( |
46
|
|
|
'LEFT', |
47
|
|
|
(string) $table2, |
48
|
|
|
sprintf('%s = %s', $field, $table1->id) |
49
|
|
|
) |
50
|
|
|
->where(sprintf('%s IS NOT NULL', $field)); |
51
|
|
|
|
52
|
|
|
return $this; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
//Has many to many |
56
|
|
|
if ($joinTable = $table1->getJoinTable($table2)) { |
57
|
|
|
$field1 = $joinTable->getJoinField($table1); |
58
|
|
|
$field2 = $joinTable->getJoinField($table2); |
59
|
|
|
|
60
|
|
|
$this->query |
61
|
|
|
->join( |
62
|
|
|
'LEFT', |
63
|
|
|
(string) $joinTable, |
64
|
|
|
sprintf('%s = %s', $field1, $table1->id) |
65
|
|
|
) |
66
|
|
|
->where(sprintf('%s IS NOT NULL', $field2)); |
67
|
|
|
|
68
|
|
|
return $this; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
throw new RuntimeException( |
72
|
|
|
sprintf('The tables %s and %s are not related', $table1, $table2) |
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
private function applyRowRelation($row): self |
77
|
|
|
{ |
78
|
|
|
$table1 = $this->table; |
79
|
|
|
$table2 = $row->getTable(); |
80
|
|
|
|
81
|
|
|
//Has one |
82
|
|
|
if ($field = $table1->getJoinField($table2)) { |
83
|
|
|
$this->query->whereEquals([(string) $field => $row->id ?: null]); |
84
|
|
|
return $this; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
//Has many |
88
|
|
|
if ($field = $table2->getJoinField($table1)) { |
89
|
|
|
$this->query |
90
|
|
|
->join( |
91
|
|
|
'LEFT', |
92
|
|
|
(string) $table2, |
93
|
|
|
sprintf('%s = %s', $field, $table1->id) |
94
|
|
|
); |
95
|
|
|
|
96
|
|
|
$this->query->whereEquals([(string) $table2->id => $row->id ?: null]); |
97
|
|
|
return $this; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
//Has many to many |
101
|
|
|
if ($joinTable = $table1->getJoinTable($table2)) { |
102
|
|
|
$field1 = $joinTable->getJoinField($table1); |
103
|
|
|
$field2 = $joinTable->getJoinField($table2); |
104
|
|
|
|
105
|
|
|
$this->query |
106
|
|
|
->join( |
107
|
|
|
'LEFT', |
108
|
|
|
(string) $joinTable, |
109
|
|
|
sprintf('%s = %s', $field1, $table1->id) |
110
|
|
|
); |
111
|
|
|
|
112
|
|
|
$this->query->whereEquals([(string) $field2 => $row->id ?: null]); |
113
|
|
|
return $this; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
throw new RuntimeException( |
117
|
|
|
sprintf('The tables %s and %s are not related', $table1, $table2) |
118
|
|
|
); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
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: