1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the O2System PHP Framework package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
* |
8
|
|
|
* @author Steeve Andrian Salim |
9
|
|
|
* @copyright Copyright (c) Steeve Andrian Salim |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
// ------------------------------------------------------------------------ |
13
|
|
|
|
14
|
|
|
namespace O2System\Reactor\Models\Sql\Relations\Maps; |
15
|
|
|
|
16
|
|
|
// ------------------------------------------------------------------------ |
17
|
|
|
|
18
|
|
|
use O2System\Reactor\Models\Sql\Model; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class Through |
22
|
|
|
* |
23
|
|
|
* @package O2System\Reactor\Models\Sql\Relations\Maps |
24
|
|
|
*/ |
25
|
|
|
class Through |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var Model |
29
|
|
|
*/ |
30
|
|
|
public $pivotModel; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
public $pivotTable; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Reference Foreign Key |
39
|
|
|
* |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
public $pivotForeignKey; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Relation Model |
46
|
|
|
* |
47
|
|
|
* @var Model |
48
|
|
|
*/ |
49
|
|
|
public $relationModel; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Relation Table |
53
|
|
|
* |
54
|
|
|
* @var string |
55
|
|
|
*/ |
56
|
|
|
public $relationTable; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Relation Foreign Key |
60
|
|
|
* |
61
|
|
|
* @var string |
62
|
|
|
*/ |
63
|
|
|
public $relationForeignKey; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Reference Model |
67
|
|
|
* |
68
|
|
|
* @var Model |
69
|
|
|
*/ |
70
|
|
|
public $referenceModel; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Reference Table |
74
|
|
|
* |
75
|
|
|
* @var string |
76
|
|
|
*/ |
77
|
|
|
public $referenceTable; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Reference Primary Key |
81
|
|
|
* |
82
|
|
|
* @var string |
83
|
|
|
*/ |
84
|
|
|
public $referencePrimaryKey; |
85
|
|
|
|
86
|
|
|
// ------------------------------------------------------------------------ |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* IntermediateMapper constructor. |
90
|
|
|
* |
91
|
|
|
* @param Model $pivotModel |
92
|
|
|
* @param string|Model $relationModel |
93
|
|
|
* @param string|Model $intermediateModel |
94
|
|
|
* @param string|null $referenceModel |
95
|
|
|
* @param string|null $relationForeignKey |
96
|
|
|
* @param string|null $pivotForeignKey |
97
|
|
|
*/ |
98
|
|
|
public function __construct( |
99
|
|
|
Model $pivotModel, |
100
|
|
|
$pivotForeignKey = null, |
101
|
|
|
$relationModel, |
102
|
|
|
$relationForeignKey = null, |
103
|
|
|
$referenceModel = null |
104
|
|
|
) { |
105
|
|
|
$this->pivotModel =& $pivotModel; |
106
|
|
|
$this->pivotTable = $pivotModel->table; |
107
|
|
|
$this->pivotForeignKey = $pivotForeignKey; |
108
|
|
|
|
109
|
|
|
// Map Relation Model |
110
|
|
|
$this->mapRelationModel($relationModel); |
111
|
|
|
$this->relationForeignKey = $this->relationTable . '.' . $relationForeignKey; |
112
|
|
|
|
113
|
|
|
// Map Reference Model |
114
|
|
|
$this->mapReferenceModel($referenceModel); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
// ------------------------------------------------------------------------ |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Map Relation Model |
121
|
|
|
* |
122
|
|
|
* @param string|Model $relationModel |
123
|
|
|
* |
124
|
|
|
* @return void |
125
|
|
|
*/ |
126
|
|
|
private function mapRelationModel($relationModel) |
127
|
|
|
{ |
128
|
|
|
if ($relationModel instanceof Model) { |
129
|
|
|
$this->relationModel = $relationModel; |
130
|
|
|
$this->relationTable = $this->relationModel->table; |
131
|
|
|
} elseif (class_exists($relationModel)) { |
132
|
|
|
$this->relationModel = new $relationModel(); |
133
|
|
|
$this->relationTable = $this->relationModel->table; |
134
|
|
|
} else { |
135
|
|
|
$this->relationTable = $relationModel; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
// ------------------------------------------------------------------------ |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Map Relation Model |
143
|
|
|
* |
144
|
|
|
* @param string|Model $referenceModel |
145
|
|
|
* |
146
|
|
|
* @return void |
147
|
|
|
*/ |
148
|
|
|
private function mapReferenceModel($referenceModel) |
149
|
|
|
{ |
150
|
|
|
if ($referenceModel instanceof Model) { |
151
|
|
|
$this->referenceModel = $referenceModel; |
152
|
|
|
$this->referenceTable = $referenceModel->table; |
153
|
|
|
$this->referencePrimaryKey = $this->referenceModel->table . '.' . $this->referenceModel->primaryKey; |
154
|
|
|
} elseif (class_exists($referenceModel)) { |
155
|
|
|
$this->referenceModel = new $referenceModel(); |
156
|
|
|
$this->referenceTable = $this->referenceModel->table; |
157
|
|
|
$this->referencePrimaryKey = $this->referenceModel->table . '.' . $this->referenceModel->primaryKey; |
158
|
|
|
} else { |
159
|
|
|
$this->referenceTable = $referenceModel; |
160
|
|
|
$this->referencePrimaryKey = $this->referenceModel->table . '.' . 'id'; |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
} |