|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace CodexShaper\DBM\Database\Schema; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Schema\ForeignKeyConstraint as DoctrineForeignKey; |
|
6
|
|
|
use Doctrine\DBAL\Schema\SchemaException; |
|
7
|
|
|
|
|
8
|
|
|
class ForeignKey |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* Create foreign key. |
|
12
|
|
|
* |
|
13
|
|
|
* @param array $foreignKey |
|
14
|
|
|
* |
|
15
|
|
|
* @return \Doctrine\DBAL\Schema\ForeignKeyConstraint |
|
16
|
|
|
*/ |
|
17
|
|
|
public static function create($foreignKey) |
|
18
|
|
|
{ |
|
19
|
|
|
// Set the local table |
|
20
|
|
|
$localTable = null; |
|
21
|
|
|
if (isset($foreignKey['localTable'])) { |
|
22
|
|
|
$localTable = static::getDoctrineTable($foreignKey['localTable']); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
$localColumns = $foreignKey['localColumns']; |
|
26
|
|
|
$foreignTable = $foreignKey['foreignTable']; |
|
27
|
|
|
$foreignColumns = $foreignKey['foreignColumns']; |
|
28
|
|
|
$options = $foreignKey['options'] ?? []; |
|
29
|
|
|
|
|
30
|
|
|
// Set the name |
|
31
|
|
|
$name = isset($foreignKey['name']) ? trim($foreignKey['name']) : ''; |
|
32
|
|
|
if (empty($name)) { |
|
33
|
|
|
$table = isset($localTable) ? $localTable->getName() : null; |
|
34
|
|
|
$name = static::createName($localColumns, 'foreign', $table); |
|
35
|
|
|
} else { |
|
36
|
|
|
// $name = Identifier::validate($name, 'Foreign Key'); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
$doctrineForeignKey = new DoctrineForeignKey( |
|
40
|
|
|
$localColumns, $foreignTable, $foreignColumns, $name, $options |
|
41
|
|
|
); |
|
42
|
|
|
|
|
43
|
|
|
if (isset($localTable)) { |
|
44
|
|
|
$doctrineForeignKey->setLocalTable($localTable); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
return $doctrineForeignKey; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Get foreign key name. |
|
52
|
|
|
* |
|
53
|
|
|
* @param array $columns |
|
54
|
|
|
* @param string $type |
|
55
|
|
|
* @param string|null $table |
|
56
|
|
|
* |
|
57
|
|
|
* @return string |
|
58
|
|
|
*/ |
|
59
|
|
|
public static function createName($columns, $type, $table = null) |
|
60
|
|
|
{ |
|
61
|
|
|
$table = isset($table) ? trim($table).'_' : ''; |
|
62
|
|
|
$type = trim($type); |
|
63
|
|
|
$name = strtolower($table.implode('_', $columns).'_'.$type); |
|
64
|
|
|
|
|
65
|
|
|
return str_replace(['-', '.'], '_', $name); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Get doctrine table. |
|
70
|
|
|
* |
|
71
|
|
|
* @param string $table |
|
72
|
|
|
* |
|
73
|
|
|
* @return \Doctrine\DBAL\Schema\Table |
|
74
|
|
|
*/ |
|
75
|
|
|
public static function getDoctrineTable($table) |
|
76
|
|
|
{ |
|
77
|
|
|
$table = trim($table); |
|
78
|
|
|
|
|
79
|
|
|
if (! static::tableExists($table)) { |
|
|
|
|
|
|
80
|
|
|
throw SchemaException::tableDoesNotExist($table); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
return static::manager()->listTableDetails($table); |
|
|
|
|
|
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Get all foreignkeys as an array. |
|
88
|
|
|
* |
|
89
|
|
|
* @return array |
|
90
|
|
|
*/ |
|
91
|
|
|
public static function toArray(DoctrineForeignKey $foreignKey) |
|
92
|
|
|
{ |
|
93
|
|
|
return [ |
|
94
|
|
|
'name' => $foreignKey->getName(), |
|
95
|
|
|
'localTable' => $foreignKey->getLocalTableName(), |
|
96
|
|
|
'localColumns' => $foreignKey->getLocalColumns(), |
|
97
|
|
|
'foreignTable' => $foreignKey->getForeignTableName(), |
|
98
|
|
|
'foreignColumns' => $foreignKey->getForeignColumns(), |
|
99
|
|
|
'options' => $foreignKey->getOptions(), |
|
100
|
|
|
]; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.