InstallData not working on Magento 2.1
-
09-01-2021 - |
Question
I have an InstallData script below which works absolutely fine on Magento 2.2 but failed to install data on Magento 2.1
class InstallData implements \Magento\Framework\Setup\InstallDataInterface
{
public function install(
\Magento\Framework\Setup\ModuleDataSetupInterface $setup,
\Magento\Framework\Setup\ModuleContextInterface $context
) {
$contextInstall = $context;
$contextInstall->getVersion();
$data = [];
$statuses = [
'data_one' => __('Data One'),
'data_two' => __('Data Two'),
'data_three' => __('Data Three'),
];
foreach ($statuses as $code => $info) {
$data[] = ['status' => $code, 'label' => $info];
}
if (!$setup->tableExists('table_name')) {
$setup->getConnection()->insertArray($setup->getTable('table_name'), ['db_colum_one', 'db_column_two'], $data);
}
}
}
Can anyone help , if I am missing anything?
Solution
Problem is with your if condition.
if (!$setup->tableExists('table_name')) {
Your condition is checking if the table don't exits then run the query. if the table is created then it will not executed.I think that what happened. You solution will be change if the collection is empty then add records.
OTHER TIPS
In order to execute InstallData.php
, You must need to remove your module entry from setup_module
table than execute
php bin/magento setup:upgrade
Hope it will help you. !!
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange