<?php

use yii\db\Migration;

/**
 * Handles the creation of table `page_lang`.
 */
class m170402_140555_create_page_lang_table extends Migration
{
    /**
     * {@inheritdoc}
     */
    public function safeUp()
    {
        $this->createTable(
            'page_lang',
            [
                'page_id'=> $this->integer(32)->notNull(),
                'language_id'=> $this->integer(32)->notNull(),
                'title'=> $this->string(255)->notNull(),
                'body'=> $this->text()->notNull(),
                'alias_id'=> $this->integer(),
                'PRIMARY KEY(page_id, language_id)',
            ]
        );
        $this->createIndex('page_lang_alias_id','page_lang','alias_id',true);
    
        $this->addForeignKey(
            'fk_page_lang_alias_id',
            'page_lang',
            'alias_id',
            'alias',
            'id',
            'SET NULL',
            'CASCADE'
        );
        
        $this->addForeignKey(
            'fk_page_lang_language_id',
            'page_lang',
            'language_id',
            'language',
            'id',
            'CASCADE',
            'CASCADE'
        );
        $this->addForeignKey(
            'fk_page_lang_page_id',
            'page_lang',
            'page_id',
            'page',
            'id',
            'CASCADE',
            'CASCADE'
        );
    }

    /**
     * {@inheritdoc}
     */
    public function safeDown()
    {
        $this->dropForeignKey(
            'fk_page_lang_alias_id',
            'page_lang'
        );
        $this->dropForeignKey('fk_page_lang_language_id', 'page_lang');
        $this->dropForeignKey('fk_page_lang_page_id', 'page_lang');
        $this->dropIndex('page_lang_alias_id', 'page_lang');
        $this->dropTable('page_lang');
    }
}
