2

I am new to Magento. I want to add a column in the newsletter_subscriber table so I made a new file mysql4-upgrade-1.6.0.0-1.6.0.1.php in app/code/core/mage/newsletter_setup/

<?php
$installer = $this;
$installer->startSetup();
$installer->getConnection()->addColumn(
    $this->getTable('newsletter/subscriber'), //table name 
    'groupid',                                //column name
    'varchar(100) NOT NULL'                   //datatype definition
);

$installer->endSetup();

?>

I updated the config file:

<modules>
    <Mage_Newsletter>
        <version>1.6.0.0</version> 
    </Mage_Newsletter>
</modules>

It doesn't work, please guide what I am doing wrong

dotancohen
  • 30,064
  • 36
  • 138
  • 197
Meshan Raza
  • 89
  • 2
  • 13

3 Answers3

6

It is not recommended to add/modify or do changes to any core files . Better you make a new module to add an extra column .

You have to mention correct version for module upgrade in app/code/local/your/module/sql/your_module_setup/upgrade-0.1.2-0.1.3.php file. (This means your upgrade the module version from 0.1.2 to 0.1.3). If your are not using upgrade script, remember to define <resources> in module config.xml and the setup script name is mysql4-install-0.1.0.php

Below is Mysql setup script file - upgrade-0.1.2-0.1.3.php

    <?php
        ini_set('display_errors', '1');

        $installer = $this;
        $installer->startSetup();
        $installer->getConnection()
                 ->addColumn(
                  $installer->getTable('newsletter/subscriber'), //Get the newsletter Table
                  'your_field_name', //New Field Name
             array(
               'type'      => Varien_Db_Ddl_Table::TYPE_TEXT, //Field Type like TYPE_INTEGER ...
               'nullable'  => true,
               'length'    => 255,
               'default'   => 'Some thing default value',
               'comment'   => 'Your field comment'
            )
        );             
        $installer->endSetup();
        ?>

and after that change app/code/local/your/module/etc/config.xml version for example

<config>
    <modules>
        <NameSpace_ModuleName>
            <version>0.1.3</version> <!-- if upgrade script version is 0.1.3 -->
        </NameSpace_ModuleName>
    </modules>
   <global>
     <resources>
        <NameSpace_ModuleName_setup>
            <setup>
                <module>NameSpace_ModuleName</module>
                <class>Mage_Catalog_Model_Resource_Setup</class>
            </setup>
            <connection>
                <use>core_setup</use>
            </connection>
        </NameSpace_ModuleName_setup>
      </resources>
   </global>
</config>
M0ns1f
  • 2,705
  • 3
  • 15
  • 25
Ajay
  • 451
  • 2
  • 13
2

With setup scripts they will be executed according to changes in the module's version.

In your case, your filename is mysql4-upgrade-1.6.0.0-1.6.0.1.php, while your version is 1.6.0.0. To get this particular script to execute you will need to bump the version to 1.6.0.1.

That being said - you're adding functionality to core Magento modules which is bad practice. You should instead move this into a local pool (app/code/local) module.

scrowler
  • 24,273
  • 9
  • 60
  • 92
0

simply you can create a script and in you dbscripts folder and run this file from terminal or web browser.

e.g save file in pub/dbscripts/filename.php paste this code and change according to your requirement

<?php
use Magento\Framework\App\Bootstrap;
require '../../app/bootstrap.php';
$bootstrap = Bootstrap::create(BP, $_SERVER);
$obj = $bootstrap->getObjectManager();
$state = $obj->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
error_reporting(E_ALL);
ini_set('display_errors', 1);
$resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
$connection = $resource->getConnection();

$salesTable = $resource->getTableName('Table_Name');
$sql = "ALTER TABLE ".$salesTable. " ADD `Field_name` varchar(255)";
$connection->query($sql);

echo"Script Run Successfully";

run this file from browser like

domain.name/pub/dbscripts/filename.php
Asad Ullah
  • 453
  • 4
  • 8