2

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?

2

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.

0

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. !!

  • already done with setup:upgrade, but no data installed – Verdu May 14 '18 at 11:00
  • Did you remove your module from table which i mention in answer !? – user67337 May 14 '18 at 11:04
  • yes! removed the module entry – Verdu May 14 '18 at 11:05
  • What's error you are getting in CLI ? – user67337 May 14 '18 at 11:06
  • No errors ! setup:upgrade is successfull – Verdu May 14 '18 at 11:08

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.