m160712_130834_create_road_surface.php 2.78 KB
<?php

use yii\db\Migration;

/**
 * Handles the creation for table `road_surface`.
 */
class m160712_130834_create_road_surface extends Migration
{
    /**
     * @inheritdoc
     */
    public function up()
    {
        $this->createTable('road_surface', [
            'road_surface_id' => $this->primaryKey()->comment('Індекс'),
            'road_id' => $this->integer()->comment('Дорога'),
            'region_id' => $this->integer()->comment('Область'),
            'road_direction_id' => $this->integer()->comment('Напрямок смуги руху'),
            'begin' => $this->float()->comment('Місцезнаходження, км+ початок'),
            'end' => $this->float()->comment('Місцезнаходження, км+ кінець'),
            'surface_type_id' => $this->integer()->comment('Тип покриття'),
            'surface_treatment_id' => $this->integer()->comment('Тип поверхневої обробки'),
            'state_common_id' => $this->integer()->comment('Стан покриття'),
        ]);
        $this->createTable('surface_treatment', [
            'surface_treatment_id' => $this->primaryKey(),
            'name' => $this->string(),
        ]);
        $this->batchInsert('surface_treatment', [ 'name' ], [
            [ 'нема' ],
        ]);
        $this->addForeignKey('road_surface_road_direction', 'road_surface', 'road_direction_id', 'road_direction', 'road_direction_id', 'CASCADE', 'CASCADE');
        $this->addForeignKey('road_surface_region', 'road_surface', 'region_id', 'region', 'region_id', 'CASCADE', 'CASCADE');
        $this->addForeignKey('road_surface_road', 'road_surface', 'road_id', 'road', 'road_id', 'CASCADE', 'CASCADE');
        $this->addForeignKey('road_surface_surface_type', 'road_surface', 'surface_type_id', 'surface_type', 'surface_type_id', 'CASCADE', 'CASCADE');
        $this->addForeignKey('road_surface_state_common', 'road_surface', 'state_common_id', 'state_common', 'state_common_id', 'CASCADE', 'CASCADE');
        $this->addForeignKey('road_surface_surface_treatment', 'road_surface', 'surface_treatment_id', 'surface_treatment', 'surface_treatment_id', 'CASCADE', 'CASCADE');
    }

    /**
     * @inheritdoc
     */
    public function down()
    {
        $this->dropForeignKey('road_surface_road_direction', 'road_surface');
        $this->dropForeignKey('road_surface_region', 'road_surface');
        $this->dropForeignKey('road_surface_road', 'road_surface');
        $this->dropForeignKey('road_surface_surface_type', 'road_surface');
        $this->dropForeignKey('road_surface_state_common', 'road_surface');
        $this->dropForeignKey('road_surface_surface_treatment', 'road_surface');
        $this->dropTable('road_surface');
        $this->dropTable('surface_treatment');
    }
}