forked from scherersoftware/cakephp-schema
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Table.php
65 lines (58 loc) · 1.62 KB
/
Table.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
declare(strict_types=1);
namespace Schema;
use Cake\Database\Connection;
use Cake\Database\Schema\TableSchema;
/**
* Custom table object for better manipulation with foreign keys.
*/
class Table extends TableSchema
{
/**
* Foreign keys constraints
*
* @var array<mixed>
*/
protected $_foreignKeys = [];
/**
* Generate the SQL to create the Table without foreign keys.
*
* Uses the connection to access the schema dialect
* to generate platform specific SQL.
*
* @param \Cake\Database\Connection $connection The connection to generate SQL for.
* @return array<string> List of SQL statements to create the table and the
* required indexes.
*/
public function createSql(Connection $connection): array
{
$this->_extractForeignKeys();
return parent::createSql($connection);
}
/**
* Returns list of ALTER TABLE statements to add foreign key constraints.
*
* @return void
*/
public function restoreForeignKeys()
{
foreach ($this->_foreignKeys as $name => $attrs) {
$this->_constraints[$name] = $attrs;
}
}
/**
* Refresh the protected foreign keys variable.
* All foreign keys are removed from the original constraints.
*
* @return void
*/
protected function _extractForeignKeys()
{
foreach ($this->_constraints as $name => $attrs) {
if ($attrs['type'] === static::CONSTRAINT_FOREIGN) {
$this->_foreignKeys[$name] = $attrs;
unset($this->_constraints[$name]);
}
}
}
}