|
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.9.8 |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace Quantum\Libraries\Database\Adapters\Sleekdb\Statements; |
|
16
|
|
|
|
|
17
|
|
|
use Quantum\Libraries\Database\Adapters\Sleekdb\SleekDbal; |
|
18
|
|
|
use Quantum\Libraries\Database\Contracts\DbalInterface; |
|
19
|
|
|
use SleekDB\Exceptions\InvalidArgumentException; |
|
20
|
|
|
use Quantum\Model\Exceptions\ModelException; |
|
21
|
|
|
use Quantum\Model\QtModel; |
|
22
|
|
|
use SleekDB\QueryBuilder; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Trait Join |
|
26
|
|
|
* @package Quantum\Libraries\Database |
|
27
|
|
|
*/ |
|
28
|
|
|
trait Join |
|
29
|
|
|
{ |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @inheritDoc |
|
33
|
|
|
*/ |
|
34
|
|
|
public function joinTo(QtModel $model, bool $switch = true): DbalInterface |
|
35
|
|
|
{ |
|
36
|
|
|
$this->addJoin(__FUNCTION__, $model, $switch); |
|
37
|
|
|
return $this; |
|
|
|
|
|
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @inheritDoc |
|
42
|
|
|
*/ |
|
43
|
|
|
public function joinThrough(QtModel $model, bool $switch = true): DbalInterface |
|
44
|
|
|
{ |
|
45
|
|
|
$this->addJoin(__FUNCTION__, $model, $switch); |
|
46
|
|
|
return $this; |
|
|
|
|
|
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Adds join |
|
51
|
|
|
* @param string $type |
|
52
|
|
|
* @param QtModel $model |
|
53
|
|
|
* @param bool $switch |
|
54
|
|
|
*/ |
|
55
|
|
|
private function addJoin(string $type, QtModel $model, bool $switch = true) |
|
56
|
|
|
{ |
|
57
|
|
|
$this->joins[] = [ |
|
|
|
|
|
|
58
|
|
|
'type' => $type, |
|
59
|
|
|
'model' => serialize($model), |
|
60
|
|
|
'switch' => $switch, |
|
61
|
|
|
]; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Starts to apply joins |
|
66
|
|
|
* @throws ModelException |
|
67
|
|
|
*/ |
|
68
|
|
|
private function applyJoins() |
|
69
|
|
|
{ |
|
70
|
|
|
if (isset($this->joins[0])) { |
|
71
|
|
|
$this->applyJoin($this->queryBuilder, $this, $this->joins[0]); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Apply the join to query builder |
|
77
|
|
|
* @param QueryBuilder $queryBuilder |
|
78
|
|
|
* @param SleekDbal $currentItem |
|
79
|
|
|
* @param array $nextItem |
|
80
|
|
|
* @param int $level |
|
81
|
|
|
* @return QueryBuilder |
|
82
|
|
|
* @throws ModelException |
|
83
|
|
|
*/ |
|
84
|
|
|
private function applyJoin(QueryBuilder $queryBuilder, SleekDbal $currentItem, array $nextItem, int $level = 1): QueryBuilder |
|
85
|
|
|
{ |
|
86
|
|
|
$modelToJoin = unserialize($nextItem['model']); |
|
87
|
|
|
$switch = $nextItem['switch']; |
|
88
|
|
|
$joinType = $nextItem['type']; |
|
89
|
|
|
|
|
90
|
|
|
$queryBuilder->join(function ($item) use ($currentItem, $modelToJoin, $switch, $joinType, $level) { |
|
91
|
|
|
|
|
92
|
|
|
$sleekModel = new self($modelToJoin->table, get_class($modelToJoin), $modelToJoin->idColumn, $modelToJoin->relations()); |
|
|
|
|
|
|
93
|
|
|
|
|
94
|
|
|
$newQueryBuilder = $sleekModel->getOrmModel()->createQueryBuilder(); |
|
|
|
|
|
|
95
|
|
|
|
|
96
|
|
|
if ($joinType == self::JOINTO) { |
|
|
|
|
|
|
97
|
|
|
$this->applyJoinTo($newQueryBuilder, $modelToJoin, $currentItem, $item); |
|
98
|
|
|
} else if ($joinType == self::JOINTHROUGH) { |
|
|
|
|
|
|
99
|
|
|
$this->applyJoinThrough($newQueryBuilder, $modelToJoin, $currentItem, $item); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
if ($switch && isset($this->joins[$level])) { |
|
103
|
|
|
$this->applyJoin($newQueryBuilder, $sleekModel, $this->joins[$level], $level + 1); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
return $newQueryBuilder; |
|
107
|
|
|
|
|
108
|
|
|
}, $modelToJoin->table); |
|
109
|
|
|
|
|
110
|
|
|
if (!$switch && isset($this->joins[$level])) { |
|
111
|
|
|
$this->applyJoin($queryBuilder, $currentItem, $this->joins[$level], $level + 1); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
return $queryBuilder; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Apply join condition for JOINTO type |
|
119
|
|
|
* @param QueryBuilder $queryBuilder |
|
120
|
|
|
* @param QtModel $modelToJoin |
|
121
|
|
|
* @param SleekDbal $currentItem |
|
122
|
|
|
* @param array $item |
|
123
|
|
|
* @return void |
|
124
|
|
|
* @throws InvalidArgumentException |
|
125
|
|
|
* @throws ModelException |
|
126
|
|
|
*/ |
|
127
|
|
|
private function applyJoinTo(QueryBuilder $queryBuilder, QtModel $modelToJoin, SleekDbal $currentItem, array $item): void |
|
128
|
|
|
{ |
|
129
|
|
|
$foreignKeys = $modelToJoin->relations(); |
|
130
|
|
|
$relatedModelName = $currentItem->getModelName(); |
|
131
|
|
|
|
|
132
|
|
|
if (!isset($foreignKeys[$relatedModelName])) { |
|
133
|
|
|
throw ModelException::wrongRelation(get_class($modelToJoin), $relatedModelName); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
$queryBuilder->where([ |
|
137
|
|
|
$foreignKeys[$relatedModelName]['foreign_key'], |
|
138
|
|
|
'=', |
|
139
|
|
|
$item[$foreignKeys[$relatedModelName]['local_key']] |
|
140
|
|
|
]); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Apply join condition for JOINTHROUGH type |
|
145
|
|
|
* @param QueryBuilder $queryBuilder |
|
146
|
|
|
* @param QtModel $modelToJoin |
|
147
|
|
|
* @param SleekDbal $currentItem |
|
148
|
|
|
* @param array $item |
|
149
|
|
|
* @return void |
|
150
|
|
|
* @throws ModelException |
|
151
|
|
|
* @throws InvalidArgumentException |
|
152
|
|
|
*/ |
|
153
|
|
|
private function applyJoinThrough(QueryBuilder $queryBuilder, QtModel $modelToJoin, SleekDbal $currentItem, array $item): void |
|
154
|
|
|
{ |
|
155
|
|
|
$foreignKeys = $currentItem->getForeignKeys(); |
|
156
|
|
|
$relatedModelName = get_class($modelToJoin); |
|
157
|
|
|
|
|
158
|
|
|
if (!isset($foreignKeys[$relatedModelName])) { |
|
159
|
|
|
throw ModelException::wrongRelation($relatedModelName, $currentItem->getModelName()); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
$queryBuilder->where([ |
|
163
|
|
|
$foreignKeys[$relatedModelName]['local_key'], |
|
164
|
|
|
'=', |
|
165
|
|
|
$item[$foreignKeys[$relatedModelName]['foreign_key']] |
|
166
|
|
|
]); |
|
167
|
|
|
} |
|
168
|
|
|
} |