2

What is the best practice to extend / modified structure database? Let say we want to add some field in customer_entity table, is it violate the rules, or we have to add new table?

I didn't find any guide documentation about it.

3

If you want to add new column into customer_entity table you must have to do InstallSchema operation.

Under your module Setup folder you have to define InstallSchema.php file.

just demo for InstallSchema.php file,

<?php

    namespace Package\Modulename\Setup;

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

    class InstallSchema implements InstallSchemaInterface
    {
        public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
        {
            $installer = $setup;

            $installer->startSetup();
            $installer->getConnection()->addColumn(
                $installer->getTable('customer_entity'),
                'custom_field1',
                [
                    'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                    'length'    => 128,
                    'unsigned' => true,
                    'nullable' => false,
                    'default' => '0',
                    'comment' => 'Custom field description'
                ]
            );
            //for second custom column
             $installer->getConnection()->addColumn(
                $installer->getTable('customer_entity'),
                'custom_field2',
                [
                    'type' => \Magento\Framework\DB\Ddl\Table::TYPE_SMALLINT,
                    'unsigned' => true,
                    'nullable' => false,
                    'default' => '0',
                    'comment' => 'pass your int value'
                ]
            );
            $installer->getConnection()->createTable($table);

            $installer->endSetup();
        }
    }
0

Magento 2 provide some classes which we can use for medified DB structure

InstallSchema – This class will execute when the module is installed to setup the database structure.

InstallData – This class will execute when the module is installed to initial the data for database table.

UpgradeSchema – This class will execute when the module is upgraded to setup the database structure.

UpgradeData – This class will execute when the module is upgraded to add/remove data from table.

Create the InstallSchema.php in the path app/code/[Vendor Name]/[Module Name]/Setup/InstallSchema.php

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


class InstallSchema implements InstallSchemaInterface
{

    public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {

        $connection = $setup->getConnection();

            $column = [
                'type' => Table::TYPE_SMALLINT,
                'length' => 6,
                'nullable' => false,
                'comment' => 'Gift Charge Enable',
                'default' => '0'
            ];
            $connection->addColumn($setup->getTable('sales_order'), 'giftcharge_enable', $column);
        }
}

After adding this column in the setup file, run the following command: php bin/magento setup:upgrade

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.