1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Quantum PHP Framework |
5
|
|
|
* |
6
|
|
|
* An open source software development framework for PHP |
7
|
|
|
* |
8
|
|
|
* @package Quantum |
9
|
|
|
* @author Arman Ag. <[email protected]> |
10
|
|
|
* @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org) |
11
|
|
|
* @link http://quantum.softberg.org/ |
12
|
|
|
* @since 2.4.0 |
13
|
|
|
*/ |
14
|
|
|
namespace Quantum\Libraries\Database\Statements; |
15
|
|
|
|
16
|
|
|
use Quantum\Libraries\Database\IdiormPatch; |
17
|
|
|
use Quantum\Mvc\QtModel; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Trait Join |
21
|
|
|
* @package Quantum\Libraries\Database\Statements |
22
|
|
|
*/ |
23
|
|
|
Trait Join |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* Adds a simple JOIN source to the query |
27
|
|
|
* @inheritDoc |
28
|
|
|
*/ |
29
|
|
|
public function join(string $table, array $constraint, string $tableAlias = null): object |
30
|
|
|
{ |
31
|
|
|
return $this->ormObject->join($table, $constraint, $tableAlias); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Adds an INNER JOIN source to the query |
36
|
|
|
* @inheritDoc |
37
|
|
|
*/ |
38
|
|
|
public function innerJoin(string $table, array $constraint, ?string $tableAlias = null): object |
39
|
|
|
{ |
40
|
|
|
return $this->ormObject->inner_join($table, $constraint, $tableAlias); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Adds an LEFT JOIN source to the query |
45
|
|
|
* @inheritDoc |
46
|
|
|
*/ |
47
|
|
|
public function leftJoin(string $table, array $constraint, ?string $tableAlias = null): object |
48
|
|
|
{ |
49
|
|
|
$this->ormPatch = IdiormPatch::getInstance()->setOrmObject($this->ormObject); |
|
|
|
|
50
|
|
|
return $this->ormPatch->left_join($table, $constraint, $tableAlias); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Adds an RIGHT JOIN source to the query |
55
|
|
|
* @inheritDoc |
56
|
|
|
*/ |
57
|
|
|
public function rightJoin(string $table, array $constraint, ?string $tableAlias = null): object |
58
|
|
|
{ |
59
|
|
|
$this->ormPatch = IdiormPatch::getInstance()->setOrmObject($this->ormObject); |
|
|
|
|
60
|
|
|
return $this->ormPatch->right_join($table, $constraint, $tableAlias); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Joins two models |
65
|
|
|
* @inheritDoc |
66
|
|
|
*/ |
67
|
|
|
public function joinTo(QtModel $model, bool $switch = true): object |
68
|
|
|
{ |
69
|
|
|
$resultObject = $this->ormObject->join($model->table, |
70
|
|
|
[ |
71
|
|
|
$model->table . '.' . $model->foreignKeys[$this->table], |
72
|
|
|
'=', |
73
|
|
|
$this->table . '.' . $this->idColumn |
74
|
|
|
] |
75
|
|
|
); |
76
|
|
|
|
77
|
|
|
if ($switch) { |
78
|
|
|
$this->table = $model->table; |
|
|
|
|
79
|
|
|
$this->idColumn = $model->idColumn; |
|
|
|
|
80
|
|
|
$this->foreignKeys = $model->foreignKeys; |
|
|
|
|
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $resultObject; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Joins through connector model |
88
|
|
|
* @inheritDoc |
89
|
|
|
*/ |
90
|
|
|
public function joinThrough(QtModel $model, bool $switch = true): object |
91
|
|
|
{ |
92
|
|
|
$resultObject = $this->ormObject->join($model->table, |
93
|
|
|
[ |
94
|
|
|
$model->table . '.' . $model->idColumn, |
95
|
|
|
'=', |
96
|
|
|
$this->table . '.' . $this->foreignKeys[$model->table] |
97
|
|
|
] |
98
|
|
|
); |
99
|
|
|
|
100
|
|
|
if ($switch) { |
101
|
|
|
$this->table = $model->table; |
|
|
|
|
102
|
|
|
$this->idColumn = $model->idColumn; |
|
|
|
|
103
|
|
|
$this->foreignKeys = $model->foreignKeys; |
|
|
|
|
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return $resultObject; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
} |