|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BfwSql\Queries; |
|
4
|
|
|
|
|
5
|
|
|
trait JoinTrait |
|
6
|
|
|
{ |
|
7
|
|
|
/** |
|
8
|
|
|
* Define the sql prefix to use for each join object |
|
9
|
|
|
* |
|
10
|
|
|
* @return void |
|
11
|
|
|
*/ |
|
12
|
|
|
protected function joinDefinePrefix() |
|
13
|
|
|
{ |
|
14
|
|
|
$this->queriesParts['join']->setPartPrefix('INNER JOIN'); |
|
|
|
|
|
|
15
|
|
|
$this->queriesParts['joinLeft']->setPartPrefix('LEFT JOIN'); |
|
16
|
|
|
$this->queriesParts['joinRight']->setPartPrefix('RIGHT JOIN'); |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Add a INNER JOIN to the request |
|
21
|
|
|
* |
|
22
|
|
|
* @return $this |
|
23
|
|
|
*/ |
|
24
|
|
|
public function join(...$args): self |
|
25
|
|
|
{ |
|
26
|
|
|
array_unshift($args, 'join'); |
|
27
|
|
|
return $this->createJoin(...$args); |
|
|
|
|
|
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Add a LEFT JOIN to the request |
|
32
|
|
|
* |
|
33
|
|
|
* @return $this |
|
34
|
|
|
*/ |
|
35
|
|
|
public function joinLeft(...$args): self |
|
36
|
|
|
{ |
|
37
|
|
|
array_unshift($args, 'joinLeft'); |
|
38
|
|
|
return $this->createJoin(...$args); |
|
|
|
|
|
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Add a RIGHT JOIN to the request |
|
43
|
|
|
* |
|
44
|
|
|
* @return $this |
|
45
|
|
|
*/ |
|
46
|
|
|
public function joinRight(...$args): self |
|
47
|
|
|
{ |
|
48
|
|
|
array_unshift($args, 'joinRight'); |
|
49
|
|
|
return $this->createJoin(...$args); |
|
|
|
|
|
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Add a (inner|left|right) join to the request |
|
54
|
|
|
* |
|
55
|
|
|
* @param string $joinType The name of the property in this class |
|
56
|
|
|
* where the join is add |
|
57
|
|
|
* @param string|array $table Name of the table concerned by |
|
58
|
|
|
* the join. Or an array with the table shortcut in key. |
|
59
|
|
|
* @param string $on SQL part "ON" for this join |
|
60
|
|
|
* @param string|array $columns Columns from the table joined to add in |
|
61
|
|
|
* the SELECT part of the request |
|
62
|
|
|
* |
|
63
|
|
|
* @return $this |
|
64
|
|
|
*/ |
|
65
|
|
|
protected function createJoin( |
|
66
|
|
|
string $joinType, |
|
67
|
|
|
$table, |
|
68
|
|
|
string $on, |
|
69
|
|
|
$columns = null |
|
70
|
|
|
): self { |
|
71
|
|
|
$this->queriesParts[$joinType]($table, $on, $columns); |
|
72
|
|
|
|
|
73
|
|
|
return $this; |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
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: