m160406_133247_create_junction_portfolio_and_user.php 2 KB
<?php

    use yii\db\Migration;

    /**
     * Handles the creation for table `portfolio_user`.
     * Has foreign keys to the tables:
     * - `portfolio`
     * - `user`
     */
    class m160406_133247_create_junction_portfolio_and_user extends Migration
    {

        /**
         * @inheritdoc
         */
        public function up()
        {
            $this->createTable('portfolio_user', [
                'portfolio_user_id' => $this->primaryKey(),
                'portfolio_id'      => $this->integer()
                                            ->notNull(),
                'user_id'           => $this->integer()
                                            ->notNull(),
                'position'          => $this->string(),
                'time'              => $this->integer(),
                'status'            => $this->integer()
                                            ->defaultValue(2)
                                            ->notNull(),
            ]);

            // add foreign key for table `portfolio`
            $this->addForeignKey('fk-portfolio_user-portfolio_id', 'portfolio_user', 'portfolio_id', 'portfolio', 'portfolio_id', 'CASCADE');

            // add foreign key for table `user`
            $this->addForeignKey('fk-portfolio_user-user_id', 'portfolio_user', 'user_id', 'user', 'id', 'CASCADE');

            $this->createIndex('unique_portfolio_user_key', 'portfolio_user', ['portfolio_id', 'user_id'], true);
        }

        /**
         * @inheritdoc
         */
        public function down()
        {

            $this->dropIndex('unique_portfolio_user_key', 'portfolio_user');

            // drops foreign key for table `portfolio`
            $this->dropForeignKey('fk-portfolio_user-portfolio_id', 'portfolio_user');

            // drops foreign key for table `user`
            $this->dropForeignKey('fk-portfolio_user-user_id', 'portfolio_user');

            $this->dropTable('portfolio_user');
        }
    }