<?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}}');
    }

}
