m160325_153328_file_relation_table.php 1.48 KB
<?php

    use yii\db\Migration;

    class m160325_153328_file_relation_table extends Migration
    {

        public function up()
        {
            $this->createTable('{{%file_relation}}', [
                'file_relation_id' => $this->primaryKey(),
                'file_id'          => $this->integer()
                                           ->notNull(),
                'model'            => $this->string()
                                           ->notNull(),
                'model_id'         => $this->integer()
                                           ->notNull(),
                'user_id'          => $this->integer(),
                'date_add'         => $this->timestamp()
                                           ->notNull()
                                           ->defaultExpression('NOW()'),
                'status'           => $this->smallInteger()
                                           ->defaultValue(1),
            ]);
            $this->addForeignKey('file_relation_file', '{{%file_relation}}', 'file_id', '{{%file}}', 'file_id', 'CASCADE', 'CASCADE');
            $this->addForeignKey('file_relation_user', '{{%file_relation}}', 'user_id', '{{%user}}', 'id', 'SET NULL', 'CASCADE');
        }

        public function down()
        {
            $this->dropForeignKey('file_relation_file', '{{%file_relation}}');
            $this->dropForeignKey('file_relation_user', '{{%file_relation}}');
            $this->dropTable('{{%file_relation}}');
        }
    }