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