1

I would like to modify Store Group model to add a few more columns needed by my business logic.

Magento 2 document only writes about extending models by EAV but I find that Store Group model does not support EAV.

Technically, It's possible to modify store_group table during module's Setup/InstallSchema. But is it ok, by principles, to do so?

Thanks.

1

Create Simple module and create UpgradeSchema.php file to add extra column to store_group table.

app/code/Vendorname/Packagename/Setup/UpgradeSchema.php file,

<?php
namespace Vendorname\Packagename\Setup;

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

/**
 * @codeCoverageIgnore
 */
class UpgradeSchema implements UpgradeSchemaInterface
{
    /**
     * {@inheritdoc}
     */
    public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $installer = $setup;

        $installer->startSetup();
        if (version_compare($context->getVersion(), '1.0.1') < 0) {
            $installer->getConnection()->addColumn(
                $installer->getTable('store_group'),
                'store_extra1',
                [
                    'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                    'nullable' => true,
                    'comment' => 'Store group extra 1'
                ]
            );

            $installer->getConnection()->addColumn(
                $installer->getTable('store_group'),
                'store_extra2',
                [
                    'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                    'nullable' => true,
                    'comment' => 'Store group extra 2'
                ]
            );
        }

        $installer->endSetup();
    }
}

In app/code/Vendorname/Packagename/etc/module.xml file,

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Vendorname_Packagename" setup_version="1.0.1">        
    </module>
</config>

run upgrade command,

php bin/magento setup:upgrade

| improve this answer | |
  • Thanks for example. Is this the official way to do so? In fact, I've discussed with team and we are not sure if this is a good practice because it might affect Magento upgrading. – Samrit Taechawongtham Nov 21 '16 at 3:31
  • Yes, This is the official way to upgrade table. – Rakesh Jesadiya Nov 21 '16 at 4:53
  • You can check it from core module example, vendor/magento/module-catalog/Setup/UpgradeSchema.php Also There are some other module also do same type of upgrade script in core. – Rakesh Jesadiya Nov 21 '16 at 4:55

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.