1
|
|
|
<?php
|
2
|
|
|
|
3
|
|
|
/*
|
4
|
|
|
* This file is part of the "RocketORM" package.
|
5
|
|
|
*
|
6
|
|
|
* https://github.com/RocketORM/ORM
|
7
|
|
|
*
|
8
|
|
|
* For the full license information, please view the LICENSE
|
9
|
|
|
* file that was distributed with this source code.
|
10
|
|
|
*/
|
11
|
|
|
|
12
|
|
|
namespace Rocket\ORM\Generator\Schema;
|
13
|
|
|
|
14
|
|
|
use Rocket\ORM\Model\Map\TableMap;
|
15
|
|
|
|
16
|
|
|
/**
|
17
|
|
|
* @author Sylvain Lorinet <[email protected]>
|
18
|
|
|
*/
|
19
|
|
|
class Relation
|
20
|
|
|
{
|
21
|
|
|
/**
|
22
|
|
|
* @var string
|
23
|
|
|
*/
|
24
|
|
|
public $with;
|
25
|
|
|
|
26
|
|
|
/**
|
27
|
|
|
* @var string
|
28
|
|
|
*/
|
29
|
|
|
public $phpName;
|
30
|
|
|
|
31
|
|
|
/**
|
32
|
|
|
* @var string
|
33
|
|
|
*/
|
34
|
|
|
public $local;
|
35
|
|
|
|
36
|
|
|
/**
|
37
|
|
|
* @var Column
|
38
|
|
|
*/
|
39
|
|
|
public $localColumn;
|
40
|
|
|
|
41
|
|
|
/**
|
42
|
|
|
* @var string
|
43
|
|
|
*/
|
44
|
|
|
public $foreign;
|
45
|
|
|
|
46
|
|
|
/**
|
47
|
|
|
* @var Column
|
48
|
|
|
*/
|
49
|
|
|
public $foreignColumn;
|
50
|
|
|
|
51
|
|
|
/**
|
52
|
|
|
* @var string
|
53
|
|
|
*/
|
54
|
|
|
public $onDelete = 'RESTRICT';
|
55
|
|
|
|
56
|
|
|
/**
|
57
|
|
|
* @var string
|
58
|
|
|
*/
|
59
|
|
|
public $onUpdate = 'RESTRICT';
|
60
|
|
|
|
61
|
|
|
/**
|
62
|
|
|
* @var bool
|
63
|
|
|
*/
|
64
|
|
|
public $isForeignKey;
|
65
|
|
|
|
66
|
|
|
/**
|
67
|
|
|
* @var int
|
68
|
|
|
*/
|
69
|
|
|
public $type;
|
70
|
|
|
|
71
|
|
|
/**
|
72
|
|
|
* @var Table
|
73
|
|
|
*/
|
74
|
|
|
protected $localTable;
|
75
|
|
|
|
76
|
|
|
/**
|
77
|
|
|
* @var Table
|
78
|
|
|
*/
|
79
|
|
|
protected $relatedTable;
|
80
|
|
|
|
81
|
|
|
|
82
|
|
|
/**
|
83
|
|
|
* @param string $with
|
84
|
|
|
* @param array $data
|
85
|
|
|
* @param bool $isForeignKey
|
86
|
|
|
*/
|
87
|
18 |
|
public function __construct($with, array $data, $isForeignKey = true)
|
88
|
|
|
{
|
89
|
18 |
|
$this->with = $with;
|
90
|
18 |
|
$this->phpName = $data['phpName'];
|
91
|
18 |
|
$this->local = $data['local'];
|
92
|
18 |
|
$this->foreign = $data['foreign'];
|
93
|
18 |
|
$this->onDelete = $data['onDelete'];
|
94
|
18 |
|
$this->onUpdate = $data['onUpdate'];
|
95
|
18 |
|
$this->isForeignKey = $isForeignKey;
|
96
|
|
|
|
97
|
18 |
|
if (isset($data['type'])) {
|
98
|
18 |
|
$this->type = $data['type'];
|
99
|
18 |
|
}
|
100
|
18 |
|
}
|
101
|
|
|
|
102
|
|
|
|
103
|
|
|
/**
|
104
|
|
|
* @return Table
|
105
|
|
|
*/
|
106
|
1 |
|
public function getLocalTable()
|
107
|
|
|
{
|
108
|
1 |
|
return $this->localTable;
|
109
|
|
|
}
|
110
|
|
|
|
111
|
|
|
/**
|
112
|
|
|
* @param Table $table
|
113
|
|
|
*/
|
114
|
3 |
|
public function setLocalTable(Table $table)
|
115
|
|
|
{
|
116
|
3 |
|
$this->localTable = $table;
|
117
|
3 |
|
$this->localColumn = $table->getColumn($this->local);
|
118
|
3 |
|
}
|
119
|
|
|
|
120
|
|
|
/**
|
121
|
|
|
* @return string
|
122
|
|
|
*
|
123
|
|
|
* @codeCoverageIgnore LogicException cannot be reached by a test
|
124
|
|
|
*/
|
125
|
|
View Code Duplication |
public function getTypeConstantName()
|
|
|
|
|
126
|
|
|
{
|
127
|
|
|
$reflection = new \ReflectionClass('\\Rocket\\ORM\\Model\\Map\\TableMap');
|
128
|
|
|
foreach ($reflection->getConstants() as $name => $value) {
|
129
|
|
|
if ($this->type == $value) {
|
130
|
|
|
return $name;
|
131
|
|
|
}
|
132
|
|
|
}
|
133
|
|
|
|
134
|
|
|
throw new \LogicException(
|
135
|
|
|
'Unknown value "' . $this->type . '" for constant TableMap::RELATION_TYPE_* '
|
136
|
|
|
. 'for relation "' . $this->phpName . '"'
|
137
|
|
|
);
|
138
|
|
|
}
|
139
|
|
|
|
140
|
|
|
/**
|
141
|
|
|
* @return Table
|
142
|
|
|
*/
|
143
|
1 |
|
public function getRelatedTable()
|
144
|
|
|
{
|
145
|
1 |
|
return $this->relatedTable;
|
146
|
|
|
}
|
147
|
|
|
|
148
|
|
|
/**
|
149
|
|
|
* @param Table $relatedTable
|
150
|
|
|
*/
|
151
|
3 |
|
public function setRelatedTable(Table $relatedTable)
|
152
|
|
|
{
|
153
|
3 |
|
$this->relatedTable = $relatedTable;
|
154
|
3 |
|
$this->foreignColumn = $relatedTable->getColumn($this->foreign);
|
155
|
3 |
|
}
|
156
|
|
|
|
157
|
|
|
/**
|
158
|
|
|
* @return bool
|
159
|
|
|
*/
|
160
|
1 |
|
public function isForeignKey()
|
161
|
|
|
{
|
162
|
1 |
|
return $this->isForeignKey;
|
163
|
|
|
}
|
164
|
|
|
|
165
|
|
|
/**
|
166
|
|
|
* @param bool $firstLetterUpper True if the first letter must be upper case
|
167
|
|
|
*
|
168
|
|
|
* @return string
|
169
|
|
|
*/
|
170
|
3 |
|
public function getPhpName($firstLetterUpper = true)
|
171
|
|
|
{
|
172
|
3 |
|
if (!$firstLetterUpper) {
|
173
|
3 |
|
return lcfirst($this->phpName);
|
174
|
|
|
}
|
175
|
|
|
|
176
|
3 |
|
return $this->phpName;
|
177
|
|
|
}
|
178
|
|
|
|
179
|
|
|
/**
|
180
|
|
|
* @return bool
|
181
|
|
|
*/
|
182
|
5 |
|
public function isMany()
|
183
|
|
|
{
|
184
|
|
|
return
|
185
|
5 |
|
TableMap::RELATION_TYPE_MANY_TO_ONE === $this->type
|
186
|
5 |
|
|| TableMap::RELATION_TYPE_MANY_TO_MANY === $this->type
|
187
|
5 |
|
;
|
188
|
|
|
}
|
189
|
|
|
|
190
|
|
|
/**
|
191
|
|
|
* @return Relation
|
192
|
|
|
*/
|
193
|
2 |
|
public function getRelatedRelation()
|
194
|
|
|
{
|
195
|
2 |
|
$localTableNamespace = $this->localTable->getNamespace();
|
196
|
2 |
|
foreach ($this->relatedTable->getRelations() as $relation) {
|
197
|
1 |
|
if ($localTableNamespace == $relation->relatedTable->getNamespace() &&
|
198
|
1 |
|
$relation->local == $this->foreign && $relation->foreign == $this->local
|
199
|
1 |
|
) {
|
200
|
1 |
|
return $relation;
|
201
|
|
|
}
|
202
|
1 |
|
}
|
203
|
|
|
|
204
|
1 |
|
throw new \LogicException(
|
205
|
1 |
|
'Cannot retrieve the related relation for between "' . $this->localTable->name . '" and "'
|
206
|
1 |
|
. $this->relatedTable->name . '" (relation name : "' . $this->phpName . '")'
|
207
|
1 |
|
);
|
208
|
|
|
}
|
209
|
|
|
}
|
210
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.