Today we’re going to discuss designing Sample Data module to create data for Belvg_WelcomeAgreement demo module. The case is that we use Gitlab CI to automatically create a demo for the module. Additionally, we wanted database data to be created simultaneously, so we decided to design Sample Data module using Magento 2 Way.
You might have seen Sample Data modules designed by Magento team, for example, Magento_CmsSampleData, Magento_ConfigurableSampleData, Magento_MsrpSampleData etc. Similar to these examples, we’ve designed our own module, the code of which you can find on github.
Let’s consider each file separately:
- Belvg/WelcomeAgreementSampleData/registration.php – file which records a new module in the system.
- Belvg/WelcomeAgreementSampleData/module.xml – one more file every module for Magento 2 must have. Here we specify Belvg_WelcomeAgreementSampleData dependence on Belvg_WelcomeAgreement in order to set the order of module installation.
1 2 3 4 |
<sequence> <module name="Magento_SampleData"/> <module name="Belvg_WelcomeAgreement" /> </sequence> |
- Belvg/WelcomeAgreementSampleData/Setup/InstallData.php – file which with the help of Dependency Injection (DI) creates the object installing interface Setup\SampleData\InstallerInterface.
- Belvg/WelcomeAgreementSampleData/Setup/Installer.php – class which installs the interface and и triggers data import for \Belvg\WelcomeAgreementSampleData\Model\Agreement by using fixtures/belvg_welcomeagreement_agreement.csv.
- Belvg/WelcomeAgreementSampleData/Model/Agreement.php – class which represents install method responsible for data import:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
/** * @param array $fixtures * @throws \Exception */ public function install(array $fixtures) { foreach ($fixtures as $fileName) { $fileName = $this->fixtureManager->getFixture($fileName); if (!file_exists($fileName)) { continue; } $rows = $this->csvReader->getData($fileName); $header = array_shift($rows); foreach ($rows as $row) { $data = []; foreach ($row as $key => $value) { $data[$header[$key]] = $value; } $row = $data; $this->agreementFactory->create() ->addData($row) ->setStores([ \Magento\Store\Model\Store::DEFAULT_STORE_ID, \Magento\Store\Model\Store::DISTRO_STORE_ID ]) ->save(); } } } |
We used $this->agreementFactory object delivered on via DA while being the sample of the original Belvg_WelcomeAgreement module. Agreement entity has the following structure:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
mysql> SHOW CREATE TABLE `belvg_welcomeagreement_agreement`; CREATE TABLE `belvg_welcomeagreement_agreement` ( `belvg_welcomeagreement_agreement_id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'Entity ID', `title` varchar(255) NOT NULL COMMENT 'Title', `content` text NOT NULL COMMENT 'Agreement content ', `start_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'Time Start', `finish_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'Time Finish', `creation_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'Creation Time', `update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 'Modification Time', `is_active` smallint(5) unsigned NOT NULL DEFAULT '0' COMMENT 'Is Active', PRIMARY KEY (`belvg_welcomeagreement_agreement_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Table To Store history of accept' |
Besides, belvg_welcomeagreement_agreement_store table is used for keeping agreements <-> stores.
1 2 3 4 5 6 7 8 9 10 |
mysql> SHOW CREATE TABLE `belvg_welcomeagreement_agreement_store`; CREATE TABLE `belvg_welcomeagreement_agreement_store` ( `belvg_welcomeagreement_agreement_id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'Entity ID', `store_id` smallint(5) unsigned NOT NULL COMMENT 'Store ID', PRIMARY KEY (`belvg_welcomeagreement_agreement_id`,`store_id`), KEY `BELVG_WELCOMEAGREEMENT_AGREEMENT_STORE_STORE_ID` (`store_id`), CONSTRAINT `BELVG_WELCOMEAGREEMENT_AGREEMENT_STORE_STORE_ID_STORE_STORE_ID` FOREIGN KEY (`store_id`) REFERENCES `store` (`store_id`) ON DELETE CASCADE, CONSTRAINT `FK_E536FD3FB4F1E1A48A7C442A41F74A66` FOREIGN KEY (`belvg_welcomeagreement_agreement_id`) REFERENCES `belvg_welcomeagreement_agreement` (`belvg_welcomeagreement_agreement_id`) ON DELETE CASCADE ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Table To Store Linkage `store` <-> `agreement`' |
In order to install the module, you need to run the commands (having Belvg_WelcomeAgreement module):
- php bin/magento module:enable Belvg_WelcomeAgreementSampleData
- php bin/magento setup:upgrade
Ecommerce Development
Take your online store to the next level with BelVG Ecommerce Development
Visit the page