m160304_065108_product.php
5.84 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
use yii\db\Migration;
class m160304_065108_product extends Migration
{
public function up()
{
$tableOptions = null;
if ($this->db->driverName === 'mysql') {
// Only for MySQL
$tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB';
// @todo https://habrahabr.ru/post/138947/
} elseif ($this->db->driverName === 'pgsql') {
// Only for PostgreSQL
// @todo use intarray field for tax_options
}
$this->createTable('{{%category_name}}', [
'category_name_id' => $this->primaryKey(),
'category_id' => $this->integer()->notNull(),
'value' => $this->string(250),
], $tableOptions);
$this->createTable('{{%category}}', [
'category_id' => $this->primaryKey(),
'parent_id' => $this->integer()->notNull()->defaultValue(0),
'path' => 'INT[]',
'depth' => $this->integer()->notNull()->defaultValue(0),
'alias' => $this->string(250),
'image' => $this->string(255),
'meta_title' => $this->string(255),
'meta_desc' => $this->text(),
'meta_robots' => $this->string(50),
'seo_text' => $this->text(),
'category_name_id' => $this->integer(),
'product_unit_id' => $this->integer()
], $tableOptions);
$this->addForeignKey('category_name_fkey', 'category', 'category_name_id', 'category_name', 'category_name_id', 'CASCADE', 'CASCADE');
$this->addForeignKey('category_name_category_fkey', 'category_name', 'category_id', 'category', 'category_id', 'NO ACTION', 'NO ACTION');
$this->createTable('{{%product_category}}', [
'product_id' => $this->integer()->notNull(),
'category_id' => $this->integer()->notNull(),
], $tableOptions);
$this->createTable('{{%brand_name}}', [
'brand_name_id' => $this->primaryKey(),
'brand_id' => $this->integer()->notNull(),
'value' => $this->string(250),
], $tableOptions);
$this->createTable('{{%brand}}', [
'brand_id' => $this->primaryKey(),
'brand_name_id' => $this->integer(),
'alias' => $this->string(250),
'image' => $this->string(255),
'meta_title' => $this->string(255),
'meta_desc' => $this->text(),
'meta_robots' => $this->string(50),
'seo_text' => $this->text(),
], $tableOptions);
$this->addForeignKey('brand_name_fkey', 'brand', 'brand_name_id', 'brand_name', 'brand_name_id', 'CASCADE', 'CASCADE');
$this->addForeignKey('brand_name_brand_fkey', 'brand_name', 'brand_id', 'brand', 'brand_id', 'NO ACTION', 'NO ACTION');
$this->createTable('{{%product}}', [
'product_id' => $this->primaryKey(),
'name' => $this->string(255)->notNull(),
'alias' => $this->string(255),
'brand_id' => $this->integer(),
'description' => $this->text(),
'video' => $this->text(),
], $tableOptions);
$this->addForeignKey('fki_product_id', 'product_category', 'product_id', 'product', 'product_id', 'NO ACTION', 'NO ACTION');
$this->addForeignKey('fki_category_id', 'product_category', 'category_id', 'category', 'category_id', 'NO ACTION', 'NO ACTION');
$this->addForeignKey('fki_brand_id', 'product', 'brand_id', 'brand', 'brand_id', 'NO ACTION', 'CASCADE');
$this->createTable('{{%product_variant}}', [
'product_variant_id' => $this->primaryKey(),
'product_id' => $this->integer()->notNull(),
'name' => $this->string(255)->notNull(),
'sku' => $this->string(255)->notNull(),
'price' => $this->float(),
'price_old' => $this->float(),
'stock' => $this->float(),
'product_unit_id' => $this->integer()->notNull(),
], $tableOptions);
$this->createTable('{{%product_unit}}', [
'product_unit_id' => $this->primaryKey(),
'name' => $this->string(255)->notNull(),
'code' => $this->string(50)->notNull(),
'is_default' => $this->boolean()
], $tableOptions);
//
$this->addForeignKey('product_variant_product_unit_fkey', 'product_variant', 'product_unit_id', 'product_unit', 'product_unit_id', 'CASCADE', 'NO ACTION');
$this->addForeignKey('category_product_unit_fkey', 'category', 'product_unit_id', 'product_unit', 'product_unit_id', 'NO ACTION', 'NO ACTION');
}
public function down()
{
$this->dropForeignKey('category_name_fkey', 'category');
$this->dropForeignKey('category_name_category_fkey', 'category_name');
$this->dropForeignKey('brand_name_fkey', 'brand');
$this->dropForeignKey('brand_name_brand_fkey', 'brand_name');
$this->dropForeignKey('fki_product_id', 'product_category');
$this->dropForeignKey('fki_category_id', 'product_category');
$this->dropForeignKey('fki_brand_id', 'product');
$this->dropForeignKey('product_variant_product_unit_fkey', 'product_variant');
$this->dropForeignKey('category_product_unit_fkey', 'category');
$this->dropTable('{{%category}}');
$this->dropTable('{{%category_name}}');
$this->dropTable('{{%product_category}}');
$this->dropTable('{{%product}}');
$this->dropTable('{{%product_variant}}');
$this->dropTable('{{%product_unit}}');
$this->dropTable('{{%brand_name}}');
$this->dropTable('{{%brand}}');
}
/*
// Use safeUp/safeDown to run migration code within a transaction
public function safeUp()
{
}
public function safeDown()
{
}
*/
}