|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Class Migration |
|
4
|
|
|
* |
|
5
|
|
|
* @link https://www.icy2003.com/ |
|
6
|
|
|
* @author icy2003 <[email protected]> |
|
7
|
|
|
* @copyright Copyright (c) 2017, icy2003 |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace icy2003\php\iexts\yii2\db; |
|
11
|
|
|
|
|
12
|
|
|
use icy2003\php\I; |
|
13
|
|
|
use yii\db\Migration as M; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Migration 扩展 |
|
17
|
|
|
*/ |
|
18
|
|
|
class Migration extends M |
|
19
|
|
|
{ |
|
20
|
|
|
use iSchemaBuilderTrait; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* character 设置 |
|
24
|
|
|
*/ |
|
25
|
|
|
const OPTION_CHARACTER = 'character'; |
|
26
|
|
|
/** |
|
27
|
|
|
* collate 设置 |
|
28
|
|
|
*/ |
|
29
|
|
|
const OPTION_COLLATE = 'collate'; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* engine 设置 |
|
33
|
|
|
*/ |
|
34
|
|
|
const OPTION_ENGINE = 'engine'; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* comment 设置 |
|
38
|
|
|
*/ |
|
39
|
|
|
const OPTION_COMMENT = 'comment'; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* 创建一个表 |
|
43
|
|
|
* |
|
44
|
|
|
* @param string $table |
|
45
|
|
|
* @param array $columns |
|
46
|
|
|
* @param array $options |
|
47
|
|
|
* |
|
48
|
|
|
* @return void |
|
49
|
|
|
*/ |
|
50
|
|
|
public function createTable($table, $columns, $options = []) |
|
51
|
|
|
{ |
|
52
|
|
|
if (false === $this->tableExists($table)) { |
|
53
|
|
|
if ('imysql' === $this->db->getDriverName()) { |
|
54
|
|
|
if (is_array($options)) { |
|
55
|
|
|
$tableOptions = [ |
|
56
|
|
|
sprintf('CHARACTER SET %s', I::get($options, 'character', 'utf8')), |
|
57
|
|
|
sprintf('COLLATE %s', I::get($options, 'collate', 'utf8_unicode_ci')), |
|
58
|
|
|
sprintf('ENGINE=%s', I::get($options, 'engine', 'InnoDB')), |
|
59
|
|
|
sprintf('COMMENT = "%s"', I::get($options, 'comment', '')), |
|
60
|
|
|
]; |
|
61
|
|
|
$optionString = implode(' ', $tableOptions); |
|
62
|
|
|
} else { |
|
63
|
|
|
$optionString = $options; |
|
64
|
|
|
} |
|
65
|
|
|
} else { |
|
66
|
|
|
$optionString = $options; |
|
67
|
|
|
} |
|
68
|
|
|
return parent::createTable($table, $columns, $optionString); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* 判断表是否存在 |
|
74
|
|
|
* |
|
75
|
|
|
* 支持 yii2 的 {{}} 格式 |
|
76
|
|
|
* |
|
77
|
|
|
* @param string $table |
|
78
|
|
|
* |
|
79
|
|
|
* @return boolean |
|
80
|
|
|
*/ |
|
81
|
|
|
public function tableExists($table) |
|
82
|
|
|
{ |
|
83
|
|
|
if (preg_match('/{{%(.+)}}/', $table, $matches)) { |
|
84
|
|
|
$table = $matches[1]; |
|
85
|
|
|
} |
|
86
|
|
|
return $this->db->createCommand()->tableExists($table); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|