<?php
    
    use yii\db\Migration;
    
    /**
     * Handles the creation of table `page_to_category`.
     */
    class m170718_155356_create_page_to_category_table extends Migration
    {
        /**
         * @inheritdoc
         */
        public function up()
        {
            $this->createTable(
                'page_to_category',
                [
                    'page_id'     => $this->integer(),
                    'category_id' => $this->integer(),
                ]
            );
            
            $this->addForeignKey('page_fk', 'page_to_category', 'page_id', 'page', 'id', 'CASCADE', 'CASCADE');
            
            $this->addForeignKey(
                'cat_fk',
                'page_to_category',
                'category_id',
                'page_category',
                'id',
                'CASCADE',
                'CASCADE'
            );
            
            $this->createIndex(
                'page_cat_uk',
                'page_to_category',
                [
                    'page_id',
                    'category_id',
                ],
                true
            );
        }
        
        /**
         * @inheritdoc
         */
        public function down()
        {
            $this->dropIndex(
                'page_cat_uk',
                'page_to_category'
            );
            
            $this->dropForeignKey(
                'cat_fk',
                'page_to_category'
            );
            $this->dropForeignKey('page_fk', 'page_to_category');
            
            $this->dropTable('page_to_category');
        }
    }
