<?php

use yii\db\Migration;

/**
 * Handles the creation of table `{{%check_product}}`.
 * Has foreign keys to the tables:
 *
 * - `{{%check}}`
 * - `{{%product}}`
 */
class m240110_161944_create_junction_table_for_check_and_product_tables extends Migration
{
    /**
     * {@inheritdoc}
     */
    public function safeUp()
    {
        $this->createTable('{{%check_product}}', [
            'check_id' => $this->integer(11)->notNull(),
            'product_id' => $this->integer(11)->notNull(),
            'quantity' => $this->integer(11)->defaultValue(1),
            'PRIMARY KEY(check_id, product_id)',
        ]);

        // creates index for column `check_id`
        $this->createIndex(
            '{{%idx-check_product-check_id}}',
            '{{%check_product}}',
            'check_id'
        );

        // add foreign key for table `{{%check}}`
        $this->addForeignKey(
            '{{%fk-check_product-check_id}}',
            '{{%check_product}}',
            'check_id',
            '{{%check}}',
            'id',
            'CASCADE'
        );

        // creates index for column `product_id`
        $this->createIndex(
            '{{%idx-check_product-product_id}}',
            '{{%check_product}}',
            'product_id'
        );

        // add foreign key for table `{{%product}}`
        $this->addForeignKey(
            '{{%fk-check_product-product_id}}',
            '{{%check_product}}',
            'product_id',
            '{{%product}}',
            'id',
            'CASCADE'
        );
    }

    /**
     * {@inheritdoc}
     */
    public function safeDown()
    {
        // drops foreign key for table `{{%check}}`
        $this->dropForeignKey(
            '{{%fk-check_product-check_id}}',
            '{{%check_product}}'
        );

        // drops index for column `check_id`
        $this->dropIndex(
            '{{%idx-check_product-check_id}}',
            '{{%check_product}}'
        );

        // drops foreign key for table `{{%product}}`
        $this->dropForeignKey(
            '{{%fk-check_product-product_id}}',
            '{{%check_product}}'
        );

        // drops index for column `product_id`
        $this->dropIndex(
            '{{%idx-check_product-product_id}}',
            '{{%check_product}}'
        );

        $this->dropTable('{{%check_product}}');
    }
}
