Magento 1.8 – How to Add Extra Field in Existing Table

magento-1.8

I added following codes in my app\code\local\Svi\Form\sql\form_setup\mysql4-install-0.1.0.php

<?php
$installer = $this;
$installer->startSetup();
$installer->run("
-- DROP TABLE IF EXISTS {$this->getTable('svi_form')};
CREATE TABLE {$this->getTable('svi_form')} (
  `form_id` int(11) unsigned NOT NULL auto_increment,
  `fname` varchar(255) NOT NULL default '',
  `lname` varchar(255) NOT NULL default '',
  `fathername` varchar(255) NOT NULL default '',
  `mothername` varchar(255) NOT NULL default '',
  `mobile` bigint(10) NOT NULL default '0',
  PRIMARY KEY (`form_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    ") or die("error in local/svi/form/sql folder.....");


 //Add new column to the 'svi_form' table

        $installer->getConnection()->addColumn(
        $this->getTable('svi/form'), //table name
        'address' 'varchar(255) NOT NULL DEFAULT '''  //'column name' 

//datatype definition

        );

$installer->endSetup();

app\code\local\Svi\Form\etc\config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Svi_Form>
            <version>0.1.0</version>
        </Svi_Form>
    </modules>
    <frontend>
        <routers>
            <form>
                <use>standard</use>
                <args>
                    <module>Svi_Form</module>
                    <frontName>form</frontName>
                </args>
            </form>
        </routers>
        <layout>
            <updates>
                <form>
                    <file>form.xml</file>
                </form>
            </updates>
        </layout>
    </frontend>
    <global>
        <models>
            <form>
                <class>Svi_Form_Model</class>
                <resourceModel>form_mysql4</resourceModel>
            </form>
            <form_mysql4>
                <class>Svi_Form_Model_Mysql4</class>
                <entities>
                    <form>
                        <table>svi_form</table>
                    </form>
                </entities>
            </form_mysql4>
        </models>
        <resources>
            <form_setup>
                <setup>
                    <module>Svi_Form</module>
                </setup>
                <connection>
                    <use>core_setup</use>
                </connection>
            </form_setup>
            <form_write>
                <connection>
                    <use>core_write</use>
                </connection>
            </form_write>
            <form_read>
                <connection>
                    <use>core_read</use>
                </connection>
            </form_read>
        </resources>
        <blocks>
            <form>
                <class>Svi_Form_Block</class>
            </form>
        </blocks>
        <helpers>
            <form>
                <class>Svi_Form_Helper</class>
            </form>
        </helpers>
    </global>
</config>

But it is not working. What to do?

Best Answer

When you want to update your module in terms of new database fields to provide new functionality, you need to alter your schema, change fields datatypes, introduce new columns, and so on.

This is when the upgrade script comes in handy. You can not make any direct changes via an SQL editor by running row queries, because whenever another user installs your module, that new database change will not work in their setup. It’s Magento’s recommendation to use an upgrade script for altering the database schema.

An upgrade script is similar to an install script – the only change is the name and a different version number.

we will create an upgrade script named mysql4-upgrade-0.1.0-0.1.1.php under app\code\local\Svi\Form\sql\form_setup\. Add the following code.

  <?php
$installer = $this;
$installer->startSetup();
$installer->getConnection()->addColumn(
    $this->getTable('form/form'),//table name 

    'address',      //column name
    'varchar(255) NOT NULL'  //datatype definition
    );

$installer->endSetup();

change in config.xml

 <modules>
    <Svi_Form>
        <version>0.1.1</version> // here 0.1.0 to 0.1.1
    </Svi_Form>
</modules>
Related Topic