m150401_100601_create_table__shop_person_type.php 2.93 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
<?php
/**
 * @author Semenov Alexander <semenov@skeeks.com>
 * @link http://skeeks.com/
 * @copyright 2010 SkeekS (СкикС)
 * @date 01.04.2015
 */
use yii\db\Schema;
use yii\db\Migration;

class m150401_100601_create_table__shop_person_type extends Migration
{
    public function safeUp()
    {
        $tableExist = $this->db->getTableSchema("{{%shop_person_type}}", true);
        if ($tableExist)
        {
            return true;
        }

        $tableOptions = null;
        if ($this->db->driverName === 'mysql') {
            $tableOptions = 'CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE=InnoDB';
        }

        $this->createTable("{{%shop_person_type}}", [
            'id'                    => Schema::TYPE_PK,

            'created_by'            => Schema::TYPE_INTEGER . ' NULL',
            'updated_by'            => Schema::TYPE_INTEGER . ' NULL',

            'created_at'            => Schema::TYPE_INTEGER . ' NULL',
            'updated_at'            => Schema::TYPE_INTEGER . ' NULL',

            'name'                  => Schema::TYPE_STRING . '(255) NOT NULL',

37 38
            'active'                => Schema::TYPE_SMALLINT . ' NOT NULL DEFAULT 1', //статус, активна некативна, удалено
            'form_id'               => Schema::TYPE_INTEGER . ' NULL', //Форма с данными и полями
39 40 41 42 43 44 45 46 47

        ], $tableOptions);

        $this->execute("ALTER TABLE {{%shop_person_type}} ADD INDEX(updated_by);");
        $this->execute("ALTER TABLE {{%shop_person_type}} ADD INDEX(created_by);");

        $this->execute("ALTER TABLE {{%shop_person_type}} ADD INDEX(created_at);");
        $this->execute("ALTER TABLE {{%shop_person_type}} ADD INDEX(updated_at);");

48 49
        $this->execute("ALTER TABLE {{%shop_person_type}} ADD INDEX(active);");
        $this->execute("ALTER TABLE {{%shop_person_type}} ADD INDEX(form_id);");
50 51
        $this->execute("ALTER TABLE {{%shop_person_type}} ADD INDEX(name);");

52
        $this->execute("ALTER TABLE {{%shop_person_type}} COMMENT = 'Типы плательщика';");
53 54 55 56 57 58 59 60 61 62

        $this->addForeignKey(
            'shop_person_type_created_by', "{{%shop_person_type}}",
            'created_by', '{{%cms_user}}', 'id', 'SET NULL', 'SET NULL'
        );

        $this->addForeignKey(
            'shop_person_type_updated_by', "{{%shop_person_type}}",
            'updated_by', '{{%cms_user}}', 'id', 'SET NULL', 'SET NULL'
        );
63 64 65 66 67

        $this->addForeignKey(
            'shop_person_type_form_form', "{{%shop_person_type}}",
            'form_id', '{{%form_form}}', 'id', 'RESTRICT', 'RESTRICT'
        );
68 69 70 71 72 73
    }

    public function safeDown()
    {
        $this->dropForeignKey("shop_person_type_updated_by", "{{%shop_person_type}}");
        $this->dropForeignKey("shop_person_type_updated_by", "{{%shop_person_type}}");
74
        $this->dropForeignKey("shop_person_type_form_form", "{{%shop_person_type}}");
75 76 77 78

        $this->dropTable("{{%shop_person_type}}");
    }
}