|
1
|
|
|
<?php namespace KitLoong\MigrationsGenerator\Generators; |
|
2
|
|
|
|
|
3
|
|
|
class ForeignKeyGenerator |
|
4
|
|
|
{ |
|
5
|
|
|
/** |
|
6
|
|
|
* @var string |
|
7
|
|
|
*/ |
|
8
|
|
|
protected $table; |
|
9
|
|
|
|
|
10
|
|
|
private $decorator; |
|
11
|
|
|
|
|
12
|
18 |
|
public function __construct(Decorator $decorator) |
|
13
|
|
|
{ |
|
14
|
18 |
|
$this->decorator = $decorator; |
|
15
|
18 |
|
} |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Get array of foreign keys |
|
19
|
|
|
* |
|
20
|
|
|
* @param string $table Table name |
|
21
|
|
|
* @param \Doctrine\DBAL\Schema\ForeignKeyConstraint[] $foreignKeys |
|
22
|
|
|
* @param bool $ignoreForeignKeyNames |
|
23
|
|
|
* |
|
24
|
|
|
* @return array |
|
25
|
|
|
*/ |
|
26
|
|
|
public function generate(string $table, $foreignKeys, bool $ignoreForeignKeyNames): array |
|
27
|
|
|
{ |
|
28
|
|
|
$this->table = $table; |
|
29
|
|
|
if($table == "m_event_list"){ |
|
30
|
|
|
$d = 1; |
|
|
|
|
|
|
31
|
|
|
} |
|
32
|
|
|
$fields = []; |
|
33
|
|
|
|
|
34
|
|
|
if (empty($foreignKeys)) { |
|
35
|
|
|
return []; |
|
36
|
|
|
} |
|
37
|
|
|
foreach ($foreignKeys as $foreignKey) { |
|
38
|
|
|
$references = ""; |
|
39
|
|
|
$field=""; |
|
40
|
|
|
foreach($foreignKey->getLocalColumns() as $f){ |
|
41
|
|
|
$field .="'".$f."',"; |
|
42
|
|
|
} |
|
43
|
|
|
$field = substr($field,0,-1); |
|
44
|
|
|
$field.=")"; |
|
45
|
|
|
foreach($foreignKey->getForeignColumns() as $f){ |
|
46
|
|
|
$references .="'".$f."',"; |
|
47
|
|
|
} |
|
48
|
|
|
$references = substr($references,0,-1); |
|
49
|
|
|
$references.=")"; |
|
50
|
|
|
$fields[] = [ |
|
51
|
|
|
'name' => $this->getName($foreignKey, $ignoreForeignKeyNames), |
|
52
|
|
|
'field' => $field, |
|
53
|
|
|
'references' => $references, |
|
54
|
|
|
'on' => $this->decorator->tableWithoutPrefix($foreignKey->getForeignTableName()), |
|
55
|
|
|
'onUpdate' => $foreignKey->hasOption('onUpdate') ? $foreignKey->getOption('onUpdate') : 'RESTRICT', |
|
56
|
|
|
'onDelete' => $foreignKey->hasOption('onDelete') ? $foreignKey->getOption('onDelete') : 'RESTRICT', |
|
57
|
|
|
]; |
|
58
|
|
|
} |
|
59
|
|
|
return $fields; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param \Doctrine\DBAL\Schema\ForeignKeyConstraint $foreignKey |
|
64
|
|
|
* @param bool $ignoreForeignKeyNames |
|
65
|
|
|
* |
|
66
|
|
|
* @return null|string |
|
67
|
|
|
*/ |
|
68
|
|
|
protected function getName($foreignKey, bool $ignoreForeignKeyNames): ?string |
|
69
|
|
|
{ |
|
70
|
|
|
if ($ignoreForeignKeyNames or $this->isDefaultName($foreignKey)) { |
|
71
|
|
|
return null; |
|
72
|
|
|
} |
|
73
|
|
|
return $foreignKey->getName(); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @param \Doctrine\DBAL\Schema\ForeignKeyConstraint $foreignKey |
|
78
|
|
|
* |
|
79
|
|
|
* @return bool |
|
80
|
|
|
*/ |
|
81
|
|
|
protected function isDefaultName($foreignKey): bool |
|
82
|
|
|
{ |
|
83
|
|
|
return $foreignKey->getName() === $this->createIndexName($foreignKey->getLocalColumns()[0]); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Create a default index name for the table. |
|
88
|
|
|
* |
|
89
|
|
|
* @param string $column |
|
90
|
|
|
* @return string |
|
91
|
|
|
*/ |
|
92
|
|
|
protected function createIndexName(string $column): string |
|
93
|
|
|
{ |
|
94
|
|
|
$index = strtolower($this->table.'_'.$column.'_foreign'); |
|
95
|
|
|
|
|
96
|
|
|
return str_replace(['-', '.'], '_', $index); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|