-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMigration.php
105 lines (93 loc) · 3.44 KB
/
Migration.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
namespace darealfive\base;
use yii\base\Event;
/**
* Class Migration
*
* @package darealfive\media
*/
class Migration extends \yii\db\Migration
{
public const EVENT_INIT = 'init';
public $tableOptions;
public function init()
{
parent::init();
$this->trigger(self::EVENT_INIT, new Event(['sender' => $this]));
}
/**
* Add a foreign key and build the name based on tables and columns automatically.
*
* @param string $table the table that the foreign key constraint will be added to.
* @param string|array $columns the name of the column to that the constraint will be added on. If there are multiple columns, separate them with commas or use an array.
* @param string $refTable the table that the foreign key references to.
* @param string|array $refColumns the name of the column that the foreign key references to. If there are multiple columns, separate them with commas or use an array.
* @param string $delete the ON DELETE option. Most DBMS support these options: RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL
* @param string $update the ON UPDATE option. Most DBMS support these options: RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL
*
* @see buildForeignKeyName for full documentation
*/
public function addForeignKeyAutoName($table, $columns, $refTable, $refColumns, $delete = null,
$update = null): void
{
$this->addForeignKey(
self::buildForeignKeyName($table, $columns, $refTable, $refColumns),
$table,
$columns,
$refTable,
$refColumns,
$delete,
$update
);
}
/**
* Builds foreign key name in the following schema:
* FK__<table>-<column>-<nextColumn>__<refTable>-<refColumn>-<nextRefColumn>
*
* @param $table
* @param $columns
* @param $refTable
* @param $refColumns
*
* @return string
*/
public static function buildForeignKeyName($table, $columns, $refTable, $refColumns): string
{
$columns = [$columns];
$refColumns = [$refColumns];
return sprintf('FK__%s-%s__%s-%s', $table, implode('-', $columns), $refTable, implode('-', $refColumns));
}
/**
* Add a foreign key and build the name based on tables and columns automatically.
*
*
* @param string $table the table that the foreign key constraint will be added to.
* @param string|array $columns the name of the column to that the constraint will be added on. If there are multiple columns, separate them with commas or use an array.
* @param bool $unique the table that the foreign key references to.
*
* @see buildForeignKeyName for full documentation
*/
public function createIndexAutoName($table, $columns, $unique = false): void
{
$this->createIndex(
self::buildIndexName($table, $columns),
$table,
$columns,
$unique
);
}
/**
* Builds index name in the following schema:
* IX__<table>-<column>-<nextColumn>
*
* @param $table
* @param $columns
*
* @return string
*/
public static function buildIndexName($table, $columns): string
{
$columns = (array) $columns;
return sprintf('IX__%s-%s', $table, implode('-', $columns));
}
}