1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link https://github.com/yiimaker/yii2-banner |
4
|
|
|
* @copyright Copyright (c) 2017 Yii Maker |
5
|
|
|
* @license BSD 3-Clause License |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace ymaker\banner\migrations; |
9
|
|
|
|
10
|
|
|
use yii\db\Migration; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Handles the creation of tables `banner` and `banner_translation`. |
14
|
|
|
* |
15
|
|
|
* @author Vladimir Kuprienko <[email protected]> |
16
|
|
|
* @since 1.0 |
17
|
|
|
*/ |
18
|
|
|
class m171024_132212_create_banner_entities extends Migration |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
public $primaryTableName = '{{%banner}}'; |
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
public $translationTableName = '{{%banner_translation}}'; |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @inheritdoc |
32
|
|
|
*/ |
33
|
|
|
public function safeUp() |
34
|
|
|
{ |
35
|
|
|
$tableOptions = null; |
36
|
|
|
if ($this->db->getDriverName() === 'mysql') { |
37
|
|
|
/* @link http://stackoverflow.com/questions/766809/whats-the-difference-between-utf8-general-ci-and-utf8-unicode-ci */ |
38
|
|
|
$tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB'; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
// tables |
42
|
|
|
$this->createTable( |
43
|
|
|
$this->primaryTableName, |
44
|
|
|
[ |
45
|
|
|
'id' => $this->primaryKey(), |
46
|
|
|
|
47
|
|
|
'slug' => $this->string()->notNull()->unique(), |
48
|
|
|
'published' => $this->boolean()->notNull()->defaultValue(true), |
49
|
|
|
'views_count' => $this->integer()->unsigned()->notNull()->defaultValue(0), |
50
|
|
|
|
51
|
|
|
'created_at' => $this->integer()->unsigned()->notNull()->defaultValue(0), |
52
|
|
|
'updated_at' => $this->integer()->unsigned()->notNull()->defaultValue(0), |
53
|
|
|
|
54
|
|
|
'valid_from' => $this->dateTime()->null(), |
55
|
|
|
'valid_until' => $this->dateTime()->null(), |
56
|
|
|
], |
57
|
|
|
$tableOptions |
58
|
|
|
); |
59
|
|
|
$this->createTable( |
60
|
|
|
$this->translationTableName, |
61
|
|
|
[ |
62
|
|
|
'id' => $this->primaryKey(), |
63
|
|
|
'banner_id' => $this->integer()->unsigned()->notNull(), |
64
|
|
|
|
65
|
|
|
'language' => $this->string(16)->notNull(), |
66
|
|
|
'content' => $this->text()->null(), |
67
|
|
|
'hint' => $this->string()->null(), |
68
|
|
|
|
69
|
|
|
'file_name' => $this->string()->notNull(), |
70
|
|
|
'alt' => $this->string()->null(), |
71
|
|
|
|
72
|
|
|
'link' => $this->string()->notNull()->defaultValue('#'), |
73
|
|
|
], |
74
|
|
|
$tableOptions |
75
|
|
|
); |
76
|
|
|
|
77
|
|
|
// foreign keys |
78
|
|
|
$this->addForeignKey( |
79
|
|
|
'fk-banner_translation-banner_id-banner-id', |
80
|
|
|
$this->translationTableName, |
81
|
|
|
'banner_id', |
82
|
|
|
'banner', |
83
|
|
|
'id', |
84
|
|
|
'CASCADE', |
85
|
|
|
'CASCADE' |
86
|
|
|
); |
87
|
|
|
|
88
|
|
|
// indexes |
89
|
|
|
$this->createIndex( |
90
|
|
|
'idx-banner-slug', |
91
|
|
|
$this->primaryTableName, |
92
|
|
|
'slug', |
93
|
|
|
true |
94
|
|
|
); |
95
|
|
|
$this->createIndex( |
96
|
|
|
'idx-banner_translation-banner_id', |
97
|
|
|
$this->translationTableName, |
98
|
|
|
'banner_id' |
99
|
|
|
); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @inheritdoc |
104
|
|
|
*/ |
105
|
|
|
public function safeDown() |
106
|
|
|
{ |
107
|
|
|
// foreign keys |
108
|
|
|
$this->dropForeignKey('fk-banner_translation-banner_id-banner-id', $this->translationTableName); |
109
|
|
|
// indexes |
110
|
|
|
|
111
|
|
|
$this->dropIndex('idx-banner_translation-banner_id', $this->translationTableName); |
112
|
|
|
$this->dropIndex('idx-banner-slug', $this->primaryTableName); |
113
|
|
|
|
114
|
|
|
// tables |
115
|
|
|
$this->dropTable($this->translationTableName); |
116
|
|
|
$this->dropTable($this->primaryTableName); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|