Sometimes we are required to add a custom field in our database table for our extension in magento. For this purpose we will perform it with the installer. But before moving we must have concept of creating a new module in magento, considering this point lets check out the steps in brief
- First create a module in our case we have Madison(Namespace) and Island(module)
- Then create two folders etc and sql.
- create a config.xml in etc.
- create a folder madison_island_setup in sql.
- make file name mysql4-install-0.1.0.php in sql folder.
- Create a xml file i.e., Madison_Island.xml in app/etc/modules.
Now lets see in details actually what we have to do,
After creating the config.xml write the below code in it :
<?xml version="1.0"?>
<config>
<modules>
<Madison_Island>
<version>0.1.0</version>
</Madison_Island>
</modules>
<global>
<fieldsets>
<sales_convert_quote_item>
<itemcomment>
<to_order_item>*</to_order_item>
</itemcomment>
</sales_convert_quote_item>
</fieldsets>
<models>
<island>
<class>Madison_Island_Model</class> <!-- Model class files -->
<resourceModel>island_mysql4</resourceModel> <!--Resource model -->
</island>
<island_mysql4>
<class>Madison_Island_Model_Mysql4</class>
</island_mysql4>
</models>
<resources>
<madison_island_setup>
<setup>
<module>Madison_Island</module>
</setup>
<connection>
<use>core_setup</use>
</connection>
</madison_island_setup>
<madison_island_write>
<connection>
<use>core_write</use>
</connection>
</madison_island_write>
<madison_island_read>
<connection>
<use>core_read</use>
</connection>
</madison_island_read>
</resources>
</global>
</config>
In the above our code resides in between <models></models>. Here, we have defined the model class file and resource model. Also the <version>0.1.0</version> tell the version of our extension. we must have our installer file name matched with this version as our installer file has(mysql4-install-0.1.0.php).
Now in the mysql4-install-0.1.0.php write the below code :
<?php
$installer = $this;
$installer->startSetup();
$installer->run("
ALTER TABLE {$this->getTable('sales_flat_quote_item')}
ADD COLUMN `itemcomment` VARCHAR(45) NOT NULL;
");
$installer->endSetup();
In this file we are executing the Alter query on the table 'sales_flat_quote_item' to add a new column 'Itemcomment'.
Now to tell the magento we have created a module and its active make a file Madison_Island.xml with the below code in app/etc/modules:
<?xml version="1.0"?>
<config>
<modules>
<Madison_Island>
<active>true</active>
<codePool>local</codePool>
</Madison_Island>
</modules>
</config>
The Installer gets executed when we hit our magento site url for the first time and add the field is added in the database.
0 Comment(s)