What is the best way to extend Store Group model?
-
04-10-2020 - |
Question
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.
Solution
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
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange