What is Setup Resources in Magento

What is Setup Resources in Magento?

Setup Resources are a part of your magento extension that will get executed as soon as your extension will get installed. To understand it better we can take an example:

Suppose you are creating an extension for “dealer management”. For storing your dealer details you must require a “dealers” table in your database. This table will contain all of your dealer details.

In order to meet this requirement you can follow two ways:

  1. You should provide sql query that must be run manually


  1. You should put this sql under setup resources of your extension such that as soon as your magento extension gets installed it should automatically create database table in current database.

At a conclusion: Setup resources are container of code that needs to be executed at the time of extension setup on web server.

Pre-Requisite :

*. Here we assume that you already have knowledge of custom magento extensions.

*. This article is for magento 1.7.x only. It might change for future version of magento.

In todays article I will cover only about “Altering database through setup scripts”.

Below are the necessary changes that needs to be done into your custom magento extension:

  1. For any kind of database manipulation through custom extension you need to create “sql” folder on the root path of your custom extension (i.e., app\code\local\NameSpace\CustomExtn folder)
  2. Create a setup folder called customextn _setup under sql folder.
  3. Create an install script under customextn _setup folder with name like mysql4-install-0.1.0.php where 0.1.0 is the version number written into config.xml file. And write below script into it.


$installer = $this;



-- DROP TABLE IF EXISTS {$this->getTable('custom_table')};

CREATE TABLE IF NOT EXISTS {$this->getTable('custom_table')} (


`sku` varchar(64) NOT NULL ,

`customer_id` INT NOT NULL ,

) ;




  1. Below code need to add resources code into app\code\local\NameSpace\CustomExtn/etc/config.xml .




               <module>NameSpace_CustomExtn</module>               <class>NameSpace_CustomExtn_Model_Resource_Mysql4_Setup</class>





       </ customextn_setup >



  1. Create a setup file under app/code/local/NameSpace/Customextn/Model/Mysql4 write below code into Setup.php file

class NameSpace_CustomExtn_Model_Resource_Mysql4_Setup extends Mage_Catalog_Model_Resource_Eav_Mysql4_Setup




How to run / re-run resource setup file: As soon as extension gets installed then magento creates a new row under “core_resource” table named “customextn_setup” with the version of install script. So you can take a look into database to make sure if that setup has been run so far or not.

Note: as soon as we open site on browser then magento looks for any new install script and compare it with any existing install script . If version number of this script is higher than older one then it runs again.

How to create update script :

If you have forget to make any changes during install script then still you can run an update script that will do remaining work. Update script file name is very similar to install script with minimul difference like

Name of install script: mysql4-install-1.0.0.php

Name of upgrade script: upgrade-1.0.0-1.0.1.php

Where 1.0.0 is intial version and 1.0.1 is new version. Make sure you also update your config.xml file. So that magento can identify what is current version of your upgrade and match it with install version.

Example of update database table using setup resource script (upgrade-1.0.0-1.0.1.php)

$installer = $this;


$installer -> getConnection() -> addColumn( $installer -> getTable( 'salesrule' ) , 'newref_id' , 'VARCHAR(50) DEFAULT NULL' );



The following two tabs change content below.

Chandra Shekhar

GCP Architect
Chandra Shekhar Pandey is Google certified Cloud engineer, I am Magento2 Trained developer. Having huge experience in designing cloud solution. I have around 12 years of experience with world enterprise IT companies and fortune 500 clients. During my architecture design I am always caring about high availability, fast performance and resilient system. From the programmer background I have huge experience in LAMP stack as well. Throughout my carrier I have worked on Retail, E-Learning, Video Conferencing and social media domain. The motive of creating cutehits was just to share the knowledge/solutions I get to know during my day to day life so that if possible I can help someone for same problems/solutions. CuteHits.com is a really a very effort for sharing knowledge to rest of the world. For any query/suggestion about same you can contact me on below details:- Email: shekharmca2005 at gmail.com Phone: +91-9560201363

Latest posts by Chandra Shekhar (see all)

You may also like...