Search code examples
phpmagento1

magento adding a column in existing table


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


Solution

  • 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>