0

I'm updating a custom plugin and I wish to add a country list in directory_country_region table.

First of all I edited my InstallSchema.php as here below:

/**
* Pre-existent code
*/

<?php
namespace Fattura24\AppFatturazione\Setup;

use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\App\Config\Storage\WriterInterface;

class InstallSchema implements InstallSchemaInterface
{
    public function __construct
    (
        WriterInterface $configWriter
    )
    [... ] // Unmodified code

**/**
* NEW CODE
*/

public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
        {
            $setup->startSetup();
            if ($installer->tableExists('directory_country_region')) {
                $installer->getConnection()->addIndex(
                    $installer->getTable('directory_country_region'),
                    $setup->getIdxName(
                        $installer->getTable('directory_country_region'),
                        ['default_name'],
                        \Magento\Framework\DB\Adapter\AdapterInterface::INDEX_TYPE_FULLTEXT
                    ),
                    ['default_name'],
                    \Magento\Framework\DB\Adapter\AdapterInterface::INDEX_TYPE_FULLTEXT
                );
            }
            $setup->endSetup();
        }
    }

Then in the Setup directory I added an InstallData.php like that:

<?php

/**
 *
 * @Author            
 * @Date               
 * @Last modified by:   
 * @Last Modified time: 
 */

namespace Fattura24\AppFatturazione\Setup;

use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

/**
 * Install Data
 */
class InstallData implements InstallDataInterface
{
    /**
     * {@inheritdoc}
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
     */
    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();
        $italian_regions = [
            ['country_id' => 'IT', 'code' => 'TO', 'default_name' => 'Torino'],
            ['country_id' => 'IT', 'code' => 'VC', 'default_name' => 'Vercelli'],
            ['country_id' => 'IT', 'code' => 'NO', 'default_name' => 'Novara'],
            ['country_id' => 'IT', 'code' => 'CN', 'default_name' => 'Cuneo'],
            ['country_id' => 'IT', 'code' => 'AT', 'default_name' => 'Asti'],
            ['country_id' => 'IT', 'code' => 'AL', 'default_name' => 'Alessandria'],
            ['country_id' => 'IT', 'code' => 'AO', 'default_name' => 'Aosta'],
            ['country_id' => 'IT', 'code' => 'IM', 'default_name' => 'Imperia'],
            ['country_id' => 'IT', 'code' => 'SV', 'default_name' => 'Savona'],
            ['country_id' => 'IT', 'code' => 'GE', 'default_name' => 'Genova'],
            ['country_id' => 'IT', 'code' => 'SP', 'default_name' => 'La Spezia'],
            ['country_id' => 'IT', 'code' => 'VA', 'default_name' => 'Varese'],
            ['country_id' => 'IT', 'code' => 'CO', 'default_name' => 'Como'],
            ['country_id' => 'IT', 'code' => 'SO', 'default_name' => 'Sondrio'],
            ['country_id' => 'IT', 'code' => 'MI', 'default_name' => 'Milano'],
            ['country_id' => 'IT', 'code' => 'BG', 'default_name' => 'Bergamo'],
            ['country_id' => 'IT', 'code' => 'BS', 'default_name' => 'Brescia'],
            ['country_id' => 'IT', 'code' => 'PV', 'default_name' => 'Pavia'],
            ['country_id' => 'IT', 'code' => 'CR', 'default_name' => 'Cremona'],
            ['country_id' => 'IT', 'code' => 'MN', 'default_name' => 'Mantova'],
            ['country_id' => 'IT', 'code' => 'BZ', 'default_name' => 'Bolzano/Bozen'],
            ['country_id' => 'IT', 'code' => 'TN', 'default_name' => 'Trento'],
            ['country_id' => 'IT', 'code' => 'VR', 'default_name' => 'Verona'],
            ['country_id' => 'IT', 'code' => 'VI', 'default_name' => 'Vicenza'],
            ['country_id' => 'IT', 'code' => 'BL', 'default_name' => 'Belluno'],
            ['country_id' => 'IT', 'code' => 'TV', 'default_name' => 'Treviso'],
            ['country_id' => 'IT', 'code' => 'VE', 'default_name' => 'Venezia'],
            ['country_id' => 'IT', 'code' => 'PD', 'default_name' => 'Padova'],
            ['country_id' => 'IT', 'code' => 'RO', 'default_name' => 'Rovigo'],
            ['country_id' => 'IT', 'code' => 'UD', 'default_name' => 'Udine'],
            ['country_id' => 'IT', 'code' => 'GO', 'default_name' => 'Gorizia'],
            ['country_id' => 'IT', 'code' => 'TS', 'default_name' => 'Trieste'],
            ['country_id' => 'IT', 'code' => 'PC', 'default_name' => 'Piacenza'],
            ['country_id' => 'IT', 'code' => 'PR', 'default_name' => 'Parma'],
            ['country_id' => 'IT', 'code' => 'RE', 'default_name' => 'Reggio nell\' Emilia'],
            ['country_id' => 'IT', 'code' => 'MO', 'default_name' => 'Modena'],
            ['country_id' => 'IT', 'code' => 'BO', 'default_name' => 'Bologna'],
            ['country_id' => 'IT', 'code' => 'FE', 'default_name' => 'Ferrara'],
            ['country_id' => 'IT', 'code' => 'RA', 'default_name' => 'Ravenna'],
            ['country_id' => 'IT', 'code' => 'FC', 'default_name' => 'Forlì-Cesena'],
            ['country_id' => 'IT', 'code' => 'PU', 'default_name' => 'Pesaro e Urbino'],
            ['country_id' => 'IT', 'code' => 'AN', 'default_name' => 'Ancona'],
            ['country_id' => 'IT', 'code' => 'MC', 'default_name' => 'Macerata'],
            ['country_id' => 'IT', 'code' => 'AP', 'default_name' => 'Ascoli Piceno'],
            ['country_id' => 'IT', 'code' => 'MS', 'default_name' => 'Massa-Carrara'],
            ['country_id' => 'IT', 'code' => 'LU', 'default_name' => 'Lucca'],
            ['country_id' => 'IT', 'code' => 'FI', 'default_name' => 'Firenze'],
            ['country_id' => 'IT', 'code' => 'LI', 'default_name' => 'Livorno'],
            ['country_id' => 'IT', 'code' => 'PI', 'default_name' => 'Pisa'],
            ['country_id' => 'IT', 'code' => 'AR', 'default_name' => 'Arezzo'],
            ['country_id' => 'IT', 'code' => 'SI', 'default_name' => 'Siena'],
            ['country_id' => 'IT', 'code' => 'GR', 'default_name' => 'Grosseto'],
            ['country_id' => 'IT', 'code' => 'PG', 'default_name' => 'Perugia'],
            ['country_id' => 'IT', 'code' => 'TR', 'default_name' => 'Terni'],
            ['country_id' => 'IT', 'code' => 'VT', 'default_name' => 'Viterbo'],
            ['country_id' => 'IT', 'code' => 'RI', 'default_name' => 'Rieti'],
            ['country_id' => 'IT', 'code' => 'RM', 'default_name' => 'Roma'],
            ['country_id' => 'IT', 'code' => 'LT', 'default_name' => 'Latina'],
            ['country_id' => 'IT', 'code' => 'FR', 'default_name' => 'Frosinone'],
            ['country_id' => 'IT', 'code' => 'CE', 'default_name' => 'Caserta'],
            ['country_id' => 'IT', 'code' => 'BN', 'default_name' => 'Benevento'],
            ['country_id' => 'IT', 'code' => 'NA', 'default_name' => 'Napoli'],
            ['country_id' => 'IT', 'code' => 'AV', 'default_name' => 'Avellino'],
            ['country_id' => 'IT', 'code' => 'SA', 'default_name' => 'Salerno'],
            ['country_id' => 'IT', 'code' => 'AQ', 'default_name' => 'L\' Aquila'],
            ['country_id' => 'IT', 'code' => 'TE', 'default_name' => 'Teramo'],
            ['country_id' => 'IT', 'code' => 'PE', 'default_name' => 'Pescara'],
            ['country_id' => 'IT', 'code' => 'CH', 'default_name' => 'Chieti'],
            ['country_id' => 'IT', 'code' => 'CB', 'default_name' => 'Campobasso'],
            ['country_id' => 'IT', 'code' => 'FG', 'default_name' => 'Foggia'],
            ['country_id' => 'IT', 'code' => 'BA', 'default_name' => 'Bari'],
            ['country_id' => 'IT', 'code' => 'BR', 'default_name' => 'Brindisi'],
            ['country_id' => 'IT', 'code' => 'LE', 'default_name' => 'Lecce'],
            ['country_id' => 'IT', 'code' => 'PZ', 'default_name' => 'Potenza'],
            ['country_id' => 'IT', 'code' => 'MT', 'default_name' => 'Matera'],
            ['country_id' => 'IT', 'code' => 'CS', 'default_name' => 'Cosenza'],
            ['country_id' => 'IT', 'code' => 'CZ', 'default_name' => 'Catanzaro'],
            ['country_id' => 'IT', 'code' => 'RC', 'default_name' => 'Reggio di Calabria'],
            ['country_id' => 'IT', 'code' => 'TP', 'default_name' => 'Trapani'],
            ['country_id' => 'IT', 'code' => 'PA', 'default_name' => 'Palermo'],
            ['country_id' => 'IT', 'code' => 'ME', 'default_name' => 'Messina'],
            ['country_id' => 'IT', 'code' => 'AG', 'default_name' => 'Agrigento'],
            ['country_id' => 'IT', 'code' => 'CL', 'default_name' => 'Caltanissetta'],
            ['country_id' => 'IT', 'code' => 'EN', 'default_name' => 'Enna'],
            ['country_id' => 'IT', 'code' => 'CT', 'default_name' => 'Catania'],
            ['country_id' => 'IT', 'code' => 'RG', 'default_name' => 'Ragusa'],
            ['country_id' => 'IT', 'code' => 'SR', 'default_name' => 'Siracusa'],
            ['country_id' => 'IT', 'code' => 'SS', 'default_name' => 'Sassari'],
            ['country_id' => 'IT', 'code' => 'NU', 'default_name' => 'Nuoro'],
            ['country_id' => 'IT', 'code' => 'CA', 'default_name' => 'Cagliari'],
            ['country_id' => 'IT', 'code' => 'PN', 'default_name' => 'Pordenone'],
            ['country_id' => 'IT', 'code' => 'IS', 'default_name' => 'Isernia'],
            ['country_id' => 'IT', 'code' => 'OR', 'default_name' => 'Oristano'],
            ['country_id' => 'IT', 'code' => 'BI', 'default_name' => 'Biella'],
            ['country_id' => 'IT', 'code' => 'LC', 'default_name' => 'Lecco'],
            ['country_id' => 'IT', 'code' => 'LO', 'default_name' => 'Lodi'],
            ['country_id' => 'IT', 'code' => 'RN', 'default_name' => 'Rimini'],
            ['country_id' => 'IT', 'code' => 'PO', 'default_name' => 'Prato'],
            ['country_id' => 'IT', 'code' => 'KR', 'default_name' => 'Crotone'],
            ['country_id' => 'IT', 'code' => 'VV', 'default_name' => 'Vibo Valentia'],
            ['country_id' => 'IT', 'code' => 'VB', 'default_name' => 'Verbano-Cusio-Ossola'],
            ['country_id' => 'IT', 'code' => 'OT', 'default_name' => 'Olbia-Tempio'],
            ['country_id' => 'IT', 'code' => 'OG', 'default_name' => 'Ogliastra'],
            ['country_id' => 'IT', 'code' => 'VS', 'default_name' => 'Medio Campidano'],
            ['country_id' => 'IT', 'code' => 'CI', 'default_name' => 'Carbonia-Iglesias'],
            ['country_id' => 'IT', 'code' => 'MB', 'default_name' => 'Monza e della Brianza'],
            ['country_id' => 'IT', 'code' => 'FM', 'default_name' => 'Fermo'],
            ['country_id' => 'IT', 'code' => 'BT', 'default_name' => 'Barletta-Andria-Trani'],
            ['country_id' => 'IT', 'code' => 'SU', 'default_name' => 'Sud Sardegna'],
        ];
        // insert the regions for Italy
        foreach ($italian_regions as $key => $value) {
            $select = $setup->getConnection()->select()
            ->from(['dcr' => $setup->getTable('directory_country_region')])
            ->where('dcr.country_id = ?', $value['country_id'])
            ->where('dcr.code = ?', $value['code'])
            ->where('dcr.default_name = ?', $value['default_name']);
            if (!$setup->getConnection()->fetchRow($select)) {
                $setup->getConnection()->insertForce(
                    $setup->getTable('directory_country_region'),
                    $value
                );
            }
        }
    }
}

Eventually I typed php bin/magento setup:upgrade, then php bin/magento cache:flush, but when I check the db there is no new data in it. Any idea about the solution? Thanks

edit: here below my module.xml:

<?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="Fattura24_AppFatturazione" setup_version="2.4.2"> </module>
</config>

in setup_module I have got the same version (2.4.2)

  • Could you please show you module.xml code and also show the version of your module in setup_module table of your database. – Abdul Pathan Jul 22 at 13:43
3

As I can see you have already installed your module. If your module is already installed then your InstallSchema.php & InstallData.php will not be executed. You have two options:

  1. Put your changes in UpgradeSchema.php & Create UpgradeData.php file rather than InstallData.php. Make sure you update the version in your module.xml file to 2.4.3.

  2. Simply Delete entry of your module from setup_module table and re-run the module. Since it wont file the installed module in table, it will execute InstallUpgrade.php & InstallData.php file. [But this is not recommended option].

Hope it finds helpfull

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.