| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace Doctrine\DBAL\Schema;
|
|
|
4 |
|
|
|
5 |
use Doctrine\DBAL\Platforms\AbstractPlatform;
|
|
|
6 |
|
|
|
7 |
use function array_keys;
|
|
|
8 |
use function array_map;
|
|
|
9 |
use function strrpos;
|
|
|
10 |
use function strtolower;
|
|
|
11 |
use function strtoupper;
|
|
|
12 |
use function substr;
|
|
|
13 |
|
|
|
14 |
/**
|
|
|
15 |
* An abstraction class for a foreign key constraint.
|
|
|
16 |
*/
|
|
|
17 |
class ForeignKeyConstraint extends AbstractAsset implements Constraint
|
|
|
18 |
{
|
|
|
19 |
/**
|
|
|
20 |
* Instance of the referencing table the foreign key constraint is associated with.
|
|
|
21 |
*
|
|
|
22 |
* @var Table
|
|
|
23 |
*/
|
|
|
24 |
protected $_localTable;
|
|
|
25 |
|
|
|
26 |
/**
|
|
|
27 |
* Asset identifier instances of the referencing table column names the foreign key constraint is associated with.
|
|
|
28 |
* array($columnName => Identifier)
|
|
|
29 |
*
|
|
|
30 |
* @var Identifier[]
|
|
|
31 |
*/
|
|
|
32 |
protected $_localColumnNames;
|
|
|
33 |
|
|
|
34 |
/**
|
|
|
35 |
* Table or asset identifier instance of the referenced table name the foreign key constraint is associated with.
|
|
|
36 |
*
|
|
|
37 |
* @var Table|Identifier
|
|
|
38 |
*/
|
|
|
39 |
protected $_foreignTableName;
|
|
|
40 |
|
|
|
41 |
/**
|
|
|
42 |
* Asset identifier instances of the referenced table column names the foreign key constraint is associated with.
|
|
|
43 |
* array($columnName => Identifier)
|
|
|
44 |
*
|
|
|
45 |
* @var Identifier[]
|
|
|
46 |
*/
|
|
|
47 |
protected $_foreignColumnNames;
|
|
|
48 |
|
|
|
49 |
/**
|
|
|
50 |
* Options associated with the foreign key constraint.
|
|
|
51 |
*
|
|
|
52 |
* @var mixed[]
|
|
|
53 |
*/
|
|
|
54 |
protected $_options;
|
|
|
55 |
|
|
|
56 |
/**
|
|
|
57 |
* Initializes the foreign key constraint.
|
|
|
58 |
*
|
|
|
59 |
* @param string[] $localColumnNames Names of the referencing table columns.
|
|
|
60 |
* @param Table|string $foreignTableName Referenced table.
|
|
|
61 |
* @param string[] $foreignColumnNames Names of the referenced table columns.
|
|
|
62 |
* @param string|null $name Name of the foreign key constraint.
|
|
|
63 |
* @param mixed[] $options Options associated with the foreign key constraint.
|
|
|
64 |
*/
|
|
|
65 |
public function __construct(
|
|
|
66 |
array $localColumnNames,
|
|
|
67 |
$foreignTableName,
|
|
|
68 |
array $foreignColumnNames,
|
|
|
69 |
$name = null,
|
|
|
70 |
array $options = []
|
|
|
71 |
) {
|
|
|
72 |
if ($name !== null) {
|
|
|
73 |
$this->_setName($name);
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
$this->_localColumnNames = $this->createIdentifierMap($localColumnNames);
|
|
|
77 |
|
|
|
78 |
if ($foreignTableName instanceof Table) {
|
|
|
79 |
$this->_foreignTableName = $foreignTableName;
|
|
|
80 |
} else {
|
|
|
81 |
$this->_foreignTableName = new Identifier($foreignTableName);
|
|
|
82 |
}
|
|
|
83 |
|
|
|
84 |
$this->_foreignColumnNames = $this->createIdentifierMap($foreignColumnNames);
|
|
|
85 |
$this->_options = $options;
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
/**
|
|
|
89 |
* @param string[] $names
|
|
|
90 |
*
|
|
|
91 |
* @return Identifier[]
|
|
|
92 |
*/
|
|
|
93 |
private function createIdentifierMap(array $names): array
|
|
|
94 |
{
|
|
|
95 |
$identifiers = [];
|
|
|
96 |
|
|
|
97 |
foreach ($names as $name) {
|
|
|
98 |
$identifiers[$name] = new Identifier($name);
|
|
|
99 |
}
|
|
|
100 |
|
|
|
101 |
return $identifiers;
|
|
|
102 |
}
|
|
|
103 |
|
|
|
104 |
/**
|
|
|
105 |
* Returns the name of the referencing table
|
|
|
106 |
* the foreign key constraint is associated with.
|
|
|
107 |
*
|
|
|
108 |
* @deprecated Use the table that contains the foreign key as part of its {@see Table::$_fkConstraints} instead.
|
|
|
109 |
*
|
|
|
110 |
* @return string
|
|
|
111 |
*/
|
|
|
112 |
public function getLocalTableName()
|
|
|
113 |
{
|
|
|
114 |
return $this->_localTable->getName();
|
|
|
115 |
}
|
|
|
116 |
|
|
|
117 |
/**
|
|
|
118 |
* Sets the Table instance of the referencing table
|
|
|
119 |
* the foreign key constraint is associated with.
|
|
|
120 |
*
|
|
|
121 |
* @deprecated Use the table that contains the foreign key as part of its {@see Table::$_fkConstraints} instead.
|
|
|
122 |
*
|
|
|
123 |
* @param Table $table Instance of the referencing table.
|
|
|
124 |
*
|
|
|
125 |
* @return void
|
|
|
126 |
*/
|
|
|
127 |
public function setLocalTable(Table $table)
|
|
|
128 |
{
|
|
|
129 |
$this->_localTable = $table;
|
|
|
130 |
}
|
|
|
131 |
|
|
|
132 |
/**
|
|
|
133 |
* @deprecated Use the table that contains the foreign key as part of its {@see Table::$_fkConstraints} instead.
|
|
|
134 |
*
|
|
|
135 |
* @return Table
|
|
|
136 |
*/
|
|
|
137 |
public function getLocalTable()
|
|
|
138 |
{
|
|
|
139 |
return $this->_localTable;
|
|
|
140 |
}
|
|
|
141 |
|
|
|
142 |
/**
|
|
|
143 |
* Returns the names of the referencing table columns
|
|
|
144 |
* the foreign key constraint is associated with.
|
|
|
145 |
*
|
|
|
146 |
* @return string[]
|
|
|
147 |
*/
|
|
|
148 |
public function getLocalColumns()
|
|
|
149 |
{
|
|
|
150 |
return array_keys($this->_localColumnNames);
|
|
|
151 |
}
|
|
|
152 |
|
|
|
153 |
/**
|
|
|
154 |
* Returns the quoted representation of the referencing table column names
|
|
|
155 |
* the foreign key constraint is associated with.
|
|
|
156 |
*
|
|
|
157 |
* But only if they were defined with one or the referencing table column name
|
|
|
158 |
* is a keyword reserved by the platform.
|
|
|
159 |
* Otherwise the plain unquoted value as inserted is returned.
|
|
|
160 |
*
|
|
|
161 |
* @param AbstractPlatform $platform The platform to use for quotation.
|
|
|
162 |
*
|
|
|
163 |
* @return string[]
|
|
|
164 |
*/
|
|
|
165 |
public function getQuotedLocalColumns(AbstractPlatform $platform)
|
|
|
166 |
{
|
|
|
167 |
$columns = [];
|
|
|
168 |
|
|
|
169 |
foreach ($this->_localColumnNames as $column) {
|
|
|
170 |
$columns[] = $column->getQuotedName($platform);
|
|
|
171 |
}
|
|
|
172 |
|
|
|
173 |
return $columns;
|
|
|
174 |
}
|
|
|
175 |
|
|
|
176 |
/**
|
|
|
177 |
* Returns unquoted representation of local table column names for comparison with other FK
|
|
|
178 |
*
|
|
|
179 |
* @return string[]
|
|
|
180 |
*/
|
|
|
181 |
public function getUnquotedLocalColumns()
|
|
|
182 |
{
|
|
|
183 |
return array_map([$this, 'trimQuotes'], $this->getLocalColumns());
|
|
|
184 |
}
|
|
|
185 |
|
|
|
186 |
/**
|
|
|
187 |
* Returns unquoted representation of foreign table column names for comparison with other FK
|
|
|
188 |
*
|
|
|
189 |
* @return string[]
|
|
|
190 |
*/
|
|
|
191 |
public function getUnquotedForeignColumns()
|
|
|
192 |
{
|
|
|
193 |
return array_map([$this, 'trimQuotes'], $this->getForeignColumns());
|
|
|
194 |
}
|
|
|
195 |
|
|
|
196 |
/**
|
|
|
197 |
* {@inheritdoc}
|
|
|
198 |
*
|
|
|
199 |
* @deprecated Use {@see getLocalColumns()} instead.
|
|
|
200 |
*
|
|
|
201 |
* @see getLocalColumns
|
|
|
202 |
*/
|
|
|
203 |
public function getColumns()
|
|
|
204 |
{
|
|
|
205 |
return $this->getLocalColumns();
|
|
|
206 |
}
|
|
|
207 |
|
|
|
208 |
/**
|
|
|
209 |
* Returns the quoted representation of the referencing table column names
|
|
|
210 |
* the foreign key constraint is associated with.
|
|
|
211 |
*
|
|
|
212 |
* But only if they were defined with one or the referencing table column name
|
|
|
213 |
* is a keyword reserved by the platform.
|
|
|
214 |
* Otherwise the plain unquoted value as inserted is returned.
|
|
|
215 |
*
|
|
|
216 |
* @deprecated Use {@see getQuotedLocalColumns()} instead.
|
|
|
217 |
*
|
|
|
218 |
* @see getQuotedLocalColumns
|
|
|
219 |
*
|
|
|
220 |
* @param AbstractPlatform $platform The platform to use for quotation.
|
|
|
221 |
*
|
|
|
222 |
* @return string[]
|
|
|
223 |
*/
|
|
|
224 |
public function getQuotedColumns(AbstractPlatform $platform)
|
|
|
225 |
{
|
|
|
226 |
return $this->getQuotedLocalColumns($platform);
|
|
|
227 |
}
|
|
|
228 |
|
|
|
229 |
/**
|
|
|
230 |
* Returns the name of the referenced table
|
|
|
231 |
* the foreign key constraint is associated with.
|
|
|
232 |
*
|
|
|
233 |
* @return string
|
|
|
234 |
*/
|
|
|
235 |
public function getForeignTableName()
|
|
|
236 |
{
|
|
|
237 |
return $this->_foreignTableName->getName();
|
|
|
238 |
}
|
|
|
239 |
|
|
|
240 |
/**
|
|
|
241 |
* Returns the non-schema qualified foreign table name.
|
|
|
242 |
*
|
|
|
243 |
* @return string
|
|
|
244 |
*/
|
|
|
245 |
public function getUnqualifiedForeignTableName()
|
|
|
246 |
{
|
|
|
247 |
$name = $this->_foreignTableName->getName();
|
|
|
248 |
$position = strrpos($name, '.');
|
|
|
249 |
|
|
|
250 |
if ($position !== false) {
|
|
|
251 |
$name = substr($name, $position + 1);
|
|
|
252 |
}
|
|
|
253 |
|
|
|
254 |
return strtolower($name);
|
|
|
255 |
}
|
|
|
256 |
|
|
|
257 |
/**
|
|
|
258 |
* Returns the quoted representation of the referenced table name
|
|
|
259 |
* the foreign key constraint is associated with.
|
|
|
260 |
*
|
|
|
261 |
* But only if it was defined with one or the referenced table name
|
|
|
262 |
* is a keyword reserved by the platform.
|
|
|
263 |
* Otherwise the plain unquoted value as inserted is returned.
|
|
|
264 |
*
|
|
|
265 |
* @param AbstractPlatform $platform The platform to use for quotation.
|
|
|
266 |
*
|
|
|
267 |
* @return string
|
|
|
268 |
*/
|
|
|
269 |
public function getQuotedForeignTableName(AbstractPlatform $platform)
|
|
|
270 |
{
|
|
|
271 |
return $this->_foreignTableName->getQuotedName($platform);
|
|
|
272 |
}
|
|
|
273 |
|
|
|
274 |
/**
|
|
|
275 |
* Returns the names of the referenced table columns
|
|
|
276 |
* the foreign key constraint is associated with.
|
|
|
277 |
*
|
|
|
278 |
* @return string[]
|
|
|
279 |
*/
|
|
|
280 |
public function getForeignColumns()
|
|
|
281 |
{
|
|
|
282 |
return array_keys($this->_foreignColumnNames);
|
|
|
283 |
}
|
|
|
284 |
|
|
|
285 |
/**
|
|
|
286 |
* Returns the quoted representation of the referenced table column names
|
|
|
287 |
* the foreign key constraint is associated with.
|
|
|
288 |
*
|
|
|
289 |
* But only if they were defined with one or the referenced table column name
|
|
|
290 |
* is a keyword reserved by the platform.
|
|
|
291 |
* Otherwise the plain unquoted value as inserted is returned.
|
|
|
292 |
*
|
|
|
293 |
* @param AbstractPlatform $platform The platform to use for quotation.
|
|
|
294 |
*
|
|
|
295 |
* @return string[]
|
|
|
296 |
*/
|
|
|
297 |
public function getQuotedForeignColumns(AbstractPlatform $platform)
|
|
|
298 |
{
|
|
|
299 |
$columns = [];
|
|
|
300 |
|
|
|
301 |
foreach ($this->_foreignColumnNames as $column) {
|
|
|
302 |
$columns[] = $column->getQuotedName($platform);
|
|
|
303 |
}
|
|
|
304 |
|
|
|
305 |
return $columns;
|
|
|
306 |
}
|
|
|
307 |
|
|
|
308 |
/**
|
|
|
309 |
* Returns whether or not a given option
|
|
|
310 |
* is associated with the foreign key constraint.
|
|
|
311 |
*
|
|
|
312 |
* @param string $name Name of the option to check.
|
|
|
313 |
*
|
|
|
314 |
* @return bool
|
|
|
315 |
*/
|
|
|
316 |
public function hasOption($name)
|
|
|
317 |
{
|
|
|
318 |
return isset($this->_options[$name]);
|
|
|
319 |
}
|
|
|
320 |
|
|
|
321 |
/**
|
|
|
322 |
* Returns an option associated with the foreign key constraint.
|
|
|
323 |
*
|
|
|
324 |
* @param string $name Name of the option the foreign key constraint is associated with.
|
|
|
325 |
*
|
|
|
326 |
* @return mixed
|
|
|
327 |
*/
|
|
|
328 |
public function getOption($name)
|
|
|
329 |
{
|
|
|
330 |
return $this->_options[$name];
|
|
|
331 |
}
|
|
|
332 |
|
|
|
333 |
/**
|
|
|
334 |
* Returns the options associated with the foreign key constraint.
|
|
|
335 |
*
|
|
|
336 |
* @return mixed[]
|
|
|
337 |
*/
|
|
|
338 |
public function getOptions()
|
|
|
339 |
{
|
|
|
340 |
return $this->_options;
|
|
|
341 |
}
|
|
|
342 |
|
|
|
343 |
/**
|
|
|
344 |
* Returns the referential action for UPDATE operations
|
|
|
345 |
* on the referenced table the foreign key constraint is associated with.
|
|
|
346 |
*
|
|
|
347 |
* @return string|null
|
|
|
348 |
*/
|
|
|
349 |
public function onUpdate()
|
|
|
350 |
{
|
|
|
351 |
return $this->onEvent('onUpdate');
|
|
|
352 |
}
|
|
|
353 |
|
|
|
354 |
/**
|
|
|
355 |
* Returns the referential action for DELETE operations
|
|
|
356 |
* on the referenced table the foreign key constraint is associated with.
|
|
|
357 |
*
|
|
|
358 |
* @return string|null
|
|
|
359 |
*/
|
|
|
360 |
public function onDelete()
|
|
|
361 |
{
|
|
|
362 |
return $this->onEvent('onDelete');
|
|
|
363 |
}
|
|
|
364 |
|
|
|
365 |
/**
|
|
|
366 |
* Returns the referential action for a given database operation
|
|
|
367 |
* on the referenced table the foreign key constraint is associated with.
|
|
|
368 |
*
|
|
|
369 |
* @param string $event Name of the database operation/event to return the referential action for.
|
|
|
370 |
*/
|
|
|
371 |
private function onEvent($event): ?string
|
|
|
372 |
{
|
|
|
373 |
if (isset($this->_options[$event])) {
|
|
|
374 |
$onEvent = strtoupper($this->_options[$event]);
|
|
|
375 |
|
|
|
376 |
if ($onEvent !== 'NO ACTION' && $onEvent !== 'RESTRICT') {
|
|
|
377 |
return $onEvent;
|
|
|
378 |
}
|
|
|
379 |
}
|
|
|
380 |
|
|
|
381 |
return null;
|
|
|
382 |
}
|
|
|
383 |
|
|
|
384 |
/**
|
|
|
385 |
* Checks whether this foreign key constraint intersects the given index columns.
|
|
|
386 |
*
|
|
|
387 |
* Returns `true` if at least one of this foreign key's local columns
|
|
|
388 |
* matches one of the given index's columns, `false` otherwise.
|
|
|
389 |
*
|
|
|
390 |
* @param Index $index The index to be checked against.
|
|
|
391 |
*
|
|
|
392 |
* @return bool
|
|
|
393 |
*/
|
|
|
394 |
public function intersectsIndexColumns(Index $index)
|
|
|
395 |
{
|
|
|
396 |
foreach ($index->getColumns() as $indexColumn) {
|
|
|
397 |
foreach ($this->_localColumnNames as $localColumn) {
|
|
|
398 |
if (strtolower($indexColumn) === strtolower($localColumn->getName())) {
|
|
|
399 |
return true;
|
|
|
400 |
}
|
|
|
401 |
}
|
|
|
402 |
}
|
|
|
403 |
|
|
|
404 |
return false;
|
|
|
405 |
}
|
|
|
406 |
}
|