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 Mapper |
22
|
|
|
* |
23
|
|
|
* @package O2System\Reactor\Models\Sql\Relations |
24
|
|
|
*/ |
25
|
|
|
class Reference |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* Reference Model |
29
|
|
|
* |
30
|
|
|
* @var Model |
31
|
|
|
*/ |
32
|
|
|
public $referenceModel; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Reference Table |
36
|
|
|
* |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
public $referenceTable; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Reference Primary Key |
43
|
|
|
* |
44
|
|
|
* @var string |
45
|
|
|
*/ |
46
|
|
|
public $referencePrimaryKey; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Relation Model |
50
|
|
|
* |
51
|
|
|
* @var Model |
52
|
|
|
*/ |
53
|
|
|
public $relationModel; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Relation Table |
57
|
|
|
* |
58
|
|
|
* @var string |
59
|
|
|
*/ |
60
|
|
|
public $relationTable; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Relation Foreign Key |
64
|
|
|
* |
65
|
|
|
* Foreign Key of Reference Table |
66
|
|
|
* |
67
|
|
|
* @var string|array |
68
|
|
|
*/ |
69
|
|
|
public $relationForeignKey; |
70
|
|
|
|
71
|
|
|
// ------------------------------------------------------------------------ |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Mapper constructor. |
75
|
|
|
* |
76
|
|
|
* @param Model $referenceModel |
77
|
|
|
* @param string|Model $relationModel |
78
|
|
|
* @param string|null $relationForeignKey |
79
|
|
|
* @param string|null $referencePrimaryKey |
80
|
|
|
*/ |
81
|
|
|
public function __construct( |
82
|
|
|
Model $referenceModel, |
83
|
|
|
$relationModel, |
84
|
|
|
$relationForeignKey = null, |
85
|
|
|
$referencePrimaryKey = null |
86
|
|
|
) { |
87
|
|
|
// Map Reference Model |
88
|
|
|
$this->referenceModel =& $referenceModel; |
89
|
|
|
|
90
|
|
|
// Map Reference Table |
91
|
|
|
$this->referenceTable = $referenceModel->table; |
92
|
|
|
|
93
|
|
|
// Map Reference Primary Key |
94
|
|
|
$this->referencePrimaryKey = isset($referencePrimaryKey) ? $referencePrimaryKey : $referenceModel->primaryKey; |
95
|
|
|
|
96
|
|
|
// Map Relation Model |
97
|
|
|
$this->mapRelationModel($relationModel); |
98
|
|
|
|
99
|
|
|
// Map Relation Foreign Key |
100
|
|
|
$this->relationForeignKey = $this->relationTable . '.' . (isset($relationForeignKey) ? $relationForeignKey |
101
|
|
|
: $this->mapRelationForeignKey()); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
// ------------------------------------------------------------------------ |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Map Relation Model |
108
|
|
|
* |
109
|
|
|
* @param string|Model $relationModel |
110
|
|
|
* |
111
|
|
|
* @return void |
112
|
|
|
*/ |
113
|
|
|
private function mapRelationModel($relationModel) |
114
|
|
|
{ |
115
|
|
|
if ($relationModel instanceof Model) { |
116
|
|
|
$this->relationModel = $relationModel; |
117
|
|
|
$this->relationTable = $this->relationModel->table; |
118
|
|
|
} elseif (class_exists($relationModel)) { |
119
|
|
|
$this->relationModel = new $relationModel(); |
120
|
|
|
$this->relationTable = $this->relationModel->table; |
121
|
|
|
} else { |
122
|
|
|
$this->relationTable = $relationModel; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
// ------------------------------------------------------------------------ |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Map Relation Foreign Key |
130
|
|
|
* |
131
|
|
|
* @return string |
132
|
|
|
*/ |
133
|
|
|
private function mapRelationForeignKey() |
134
|
|
|
{ |
135
|
|
|
$tablePrefixes = [ |
136
|
|
|
't_', |
137
|
|
|
'tm_', |
138
|
|
|
'tr_', |
139
|
|
|
'tb_', |
140
|
|
|
]; |
141
|
|
|
|
142
|
|
|
return $this->referencePrimaryKey . '_' . str_replace($tablePrefixes, '', $this->referenceTable); |
143
|
|
|
} |
144
|
|
|
} |