m160301_105759_chat.php 1.66 KB
<?php

use yii\db\Migration;

class m160301_105759_chat extends Migration
{
    public function up()
    {
        $this->createTable('{{%chat}}', [
            'chat_id' => $this->primaryKey(),
            'status' => $this->integer(),
            'comment' => $this->text(),
            'from_user' => $this->integer(),
            'to_user' => $this->integer(),
        ]);


        $this->createTable('{{%file}}', [
            'file_id' => $this->primaryKey(),
            'status' => $this->integer(),
            'name' => $this->string(50),
            'dir' => $this->string(255)
        ]);


        $this->createTable('{{%message}}', [
            'message_id' => $this->primaryKey(),
            'chat_id' => $this->integer(),
            'user_id' => $this->integer(),
            'status' => $this->integer(),
            'text' => $this->text(),
            'files' => $this->string(255),
            'date' => $this->timestamp()->defaultExpression('NOW()'),

        ]);


        $this->addForeignKey('chat_from_user', '{{%chat}}', 'from_user', '{{%user}}', 'id', 'SET NULL', 'NO ACTION');
        $this->addForeignKey('chat_to_user', '{{%chat}}', 'to_user', '{{%user}}', 'id', 'SET NULL', 'NO ACTION');

        $this->addForeignKey('message_to_chat', '{{%message}}', 'chat_id', '{{%chat}}', 'chat_id', 'CASCADE', 'CASCADE');
    }

    public function down()
    {
        $this->dropForeignKey('chat_from_user', '{{%chat}}');
        $this->dropForeignKey('chat_to_user', '{{%chat}}');
        $this->dropForeignKey('message_to_chat', '{{%message}}');
        $this->dropTable('{{%chat}}');
        $this->dropTable('{{%file}}');
        $this->dropTable('{{%message}}');
    }
}