1

Below class is my InstallSchema.php

<?php

namespace Test2\Helloworld\Setup;

use \Magento\Framework\Setup\InstallSchemaInterface;
use \Magento\Framework\Setup\ModuleContextInterface;
use \Magento\Framework\Setup\SchemaSetupInterface;
use \Magento\Framework\DB\Ddl\Table;

class InstallSchema implements InstallSchemaInterface
{
    public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();

        $tableName = $setup->getTable('base_price');

        if ($setup->getConnection()->isTableExists($tableName) != true) {
            $table = $setup->getConnection()
                ->newTable($tableName)
                ->addColumn(
                    'id',
                    Table::TYPE_INTEGER,
                    null,
                    [
                        'identity' => true,
                        'unsigned' => true,
                        'nullable' => false,
                        'primary' => true
                    ],
                    'ID'
                )
                ->addColumn(
                    'width',
                    Table::TYPE_INTEGER,
                    null,
                    ['nullable' => false],
                    'Width'
                )
                ->addColumn(
                    'height',
                    Table::TYPE_INTEGER,
                    null,
                    ['nullable' => false],
                    'Height'
                )
                ->addColumn(
                    'price_component',
                    Table::TYPE_INTEGER,
                    null,
                    ['nullable' => false],
                    'Price Component'
                )
                ->setComment('Test2_Helloworld - price');
            $setup->getConnection()->createTable($table);
        }

        $setup->endSetup();
    }
}

I need to add another table for color price with 3 columns id, color and price_component

Is there a way to do that?

3

You can add as many table as you want in your installSchema file. Refer below code

<?php

namespace Test2\Helloworld\Setup;

use \Magento\Framework\Setup\InstallSchemaInterface;
use \Magento\Framework\Setup\ModuleContextInterface;
use \Magento\Framework\Setup\SchemaSetupInterface;
use \Magento\Framework\DB\Ddl\Table;

class InstallSchema implements InstallSchemaInterface
{
    public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();

        $tableName = $setup->getTable('base_price');
        $colorTableName = $setup->getTable('color_price');

        if ($setup->getConnection()->isTableExists($tableName) != true) {
            $table = $setup->getConnection()
                ->newTable($tableName)
                ->addColumn(
                    'id',
                    Table::TYPE_INTEGER,
                    null,
                    [
                        'identity' => true,
                        'unsigned' => true,
                        'nullable' => false,
                        'primary' => true
                    ],
                    'ID'
                )
                ->addColumn(
                    'width',
                    Table::TYPE_INTEGER,
                    null,
                    ['nullable' => false],
                    'Width'
                )
                ->addColumn(
                    'height',
                    Table::TYPE_INTEGER,
                    null,
                    ['nullable' => false],
                    'Height'
                )
                ->addColumn(
                    'price_component',
                    Table::TYPE_INTEGER,
                    null,
                    ['nullable' => false],
                    'Price Component'
                )
                ->setComment('Test2_Helloworld - price');
            $setup->getConnection()->createTable($table);
        }

    if ($setup->getConnection()->isTableExists($colorTableName) != true) {
            $table = $setup->getConnection()
                ->newTable($colorTableName)
                ->addColumn(
                    'id',
                    Table::TYPE_INTEGER,
                    null,
                    [
                        'identity' => true,
                        'unsigned' => true,
                        'nullable' => false,
                        'primary' => true
                    ],
                    'ID'
                )
                ->addColumn(
                    'color',
                    Table::TYPE_INTEGER,
                    null,
                    ['nullable' => false],
                    'color'
                )
                ->addColumn(
                    'price_component',
                    Table::TYPE_INTEGER,
                    null,
                    ['nullable' => false],
                    'price_component '
                )
                ->setComment('Color Price');
            $setup->getConnection()->createTable($table);
        }        

        $setup->endSetup();
    }
}

In case if you have have already installed your module you can follow below two methods

Method 1

Go to your database and find table setup_modules find your modulename and remove your entry. This will initiate your setup script and you will able to see your new tables.

Method 2

Create updagradeSchema

Create a new file UpgradeSchema.php in Setup Folder and place this code

namespace Test2\Helloworld\Setup;

use Magento\Framework\Setup\UpgradeSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;

class UpgradeSchema implements UpgradeSchemaInterface
{
    /**
     * {@inheritdoc}
     */
    public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        if (version_compare($context->getVersion(), '1.0.1', '<')) {
            $this->addColorTabe($setup);
        }
    }

    /**
     * add Color Table
     *
     * @param SchemaSetupInterface $setup
     * @return $this
     */
    protected function addColorTabe(SchemaSetupInterface $setup)
    {

    $colorTableName = $setup->getTable('color_price');

    if ($setup->getConnection()->isTableExists($colorTableName) != true) {
            $table = $setup->getConnection()
                ->newTable($colorTableName)
                ->addColumn(
                    'id',
                    Table::TYPE_INTEGER,
                    null,
                    [
                        'identity' => true,
                        'unsigned' => true,
                        'nullable' => false,
                        'primary' => true
                    ],
                    'ID'
                )
                ->addColumn(
                    'color',
                    Table::TYPE_INTEGER,
                    null,
                    ['nullable' => false],
                    'color'
                )
                ->addColumn(
                    'price_component',
                    Table::TYPE_INTEGER,
                    null,
                    ['nullable' => false],
                    'price_component '
                )
                ->setComment('Color Price');
            $setup->getConnection()->createTable($table);
        }

        return $this;
    }
}

and change the setup_version to 1.0.1 in <moduledir>/etc/module.xml. This will update your module schema.

| improve this answer | |
2

Hmmm, i think you just need to copy the block :

$tableName = $setup->getTable('base_price');

    if ($setup->getConnection()->isTableExists($tableName) != true) {
        $table = $setup->getConnection()
            ->newTable($tableName)
            ->addColumn(
                'id',
                Table::TYPE_INTEGER,
                null,
                [
                    'identity' => true,
                    'unsigned' => true,
                    'nullable' => false,
                    'primary' => true
                ],
                'ID'
            )
            ->addColumn(
                'width',
                Table::TYPE_INTEGER,
                null,
                ['nullable' => false],
                'Width'
            )
            ->addColumn(
                'height',
                Table::TYPE_INTEGER,
                null,
                ['nullable' => false],
                'Height'
            )
            ->addColumn(
                'price_component',
                Table::TYPE_INTEGER,
                null,
                ['nullable' => false],
                'Price Component'
            )
            ->setComment('Test2_Helloworld - price');
        $setup->getConnection()->createTable($table);
    }

And it will create the second table, i'm not 100% sure :)

| improve this answer | |
  • I tried that but didnt work @Nundra – Hemal Aug 30 '17 at 9:41
  • As said by Userpk this should work, i just check and i do it, you are sure to use different name ? Check your variable etc... – Nundra Aug 30 '17 at 9:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.