Blame view

console/migrations/m160321_232402_orders1.php 1.87 KB
4253cbec   root   first commit
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
  <?php

  

  use yii\db\Migration;

  

  class m160321_232402_orders extends Migration

  {

      public function up()

      {

          $tableOptions = null;

          if ($this->db->driverName === 'mysql') {

              // http://stackoverflow.com/questions/766809/whats-the-difference-between-utf8-general-ci-and-utf8-unicode-ci

              $tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB';

          }

  

          $this->createTable('{{%orders}}', [

              'order_id' => $this->primaryKey(),

              'customer_id' => $this->integer(),

              'name' => $this->string()->notNull(),

              'email' => $this->string()->notNull(),

              'phone' => $this->string(32)->notNull(),

              'delivery' => $this->integer(),

              'payment' => $this->integer(),

              'code' => $this->string(),

              'status' => $this->smallInteger(),

              'created_at' => $this->integer()->notNull(),

              'updated_at' => $this->integer()->notNull(),

          ], $tableOptions);

  

          $this->createTable('{{%order_items}}', [

              'order_items_id' => $this->primaryKey(),

              'order_id' => $this->integer(),

              'item_id' => $this->integer(),

              'item_count' => $this->integer(),

              'price' => $this->float(),

          ], $tableOptions);

  

          $this->addForeignKey('orders_items_fk', '{{%order_items}}', 'order_id', '{{%orders}}', 'order_id', 'CASCADE', 'CASCADE');

          $this->addForeignKey('orders_items_items_fk', '{{%order_items}}', 'item_id', '{{%product}}', 'product_id', 'RESTRICT', 'RESTRICT');

      }

  

      public function down()

      {

          $this->dropForeignKey('orders_items_fk', '{{%order_items}}');

          $this->dropForeignKey('orders_items_items_fk', '{{%order_items}}');

          $this->dropTable('{{%orders}}');

          $this->dropTable('{{%order_items}}');

      }

  

  }