Hi Friends ! At the time of developing module in magento, I am facing problem to make installer file for my module. And finally I got the solution which I would llike to share with you.
Here I have describe in following points.
- Add the Setup Resource to our config
- Create resource class file
- Create installer script
- Create upgrade script
Adding the Setup Resource
Add following section in your config.xml under global section.
Path for your xml is : /app/code/local/YourCompany/YourModule/etc
<resources>
<!-- ... -->
<yourmodule_setup>
<setup>
<module>YourCompany_YourModule</module>
<class>YourCompany_YourModule_Model_Resource_Mysql4_Setup</class>
</setup>
<connection>
<use>core_setup</use>
</connection>
</yourmodule_setup>
<!-- ... -->
</resources>
After adding the above section to your config, clear your Magento cache and try to load any page of your Magento site. You’ll see an exception something like
Fatal error: Class 'YourCompany_YourModule_Model_Resource_Mysql4_Setup' not found in
Magento just tried to instantiate the class you specified in your config, but couldn’t find it.
Create resource class file
You’ll need to create the following file, with the following contents.
File: app/code/local/YourCompany/YourModule/Model/Resource/Mysql4/Setup.php
class YourCompany_YourModule_Model_Resource_Mysql4_Setup extends Mage_Core_Model_Resource_Setup {
}
Now, reload any page of your Magento site. The exception should be gone, and your page should load as expected.
Creating Installer Script
First, take a look at your config.xml file
<modules>
<YourCompany_YourModule>
<version>0.1.0</version>
</YourCompany_YourModule>
</modules>
The above section is required in all config.xml files, it identifies the module as well as it’s version number. Your installer script’s name will be based on this version number.The following assumes the current version of your module is 0.1.0.
Create the following file at the following location
File: app/code/local/YourCompany/YourModule/sql/yourmodule_setup/mysql4-install-0.1.0.php
echo 'Running This Upgrade: '.get_class($this)."\n <br /> \n";
die("Exit for now");
The yourmodule_setup portion of the path should match the tag you created in your config.xml file (<yourmodule_setup />). The 0.1.0 portion of the filename should match the starting version of your module. Clear your Magento cache and reload any page in your Magento site and you should see something like
Running This Upgrade: YourCompany_YourModule_Model_Resource_Mysql4_Setup
Exit for now
...
Which means your update script ran. Just remove the die statement. And write the actual code something like
$installer = $this;
$installer->startSetup();
$installer->run("
CREATE TABLE `{$installer->getTable('custom_tablename')}` (
`id` int(11) NOT NULL auto_increment,
`first_name` varchar(32) NOT NULL,
`last_name` varchar(32) NOT NULL,
`created_date` datetime default NULL,
`updated_date` datetime default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `{$installer->getTable('custom_tablename')}` VALUES (1,'Your Firstname','Yor Lastname','2009-07-01 00:00:00','2009-07-02 23:12:30');
");
$installer->endSetup();
You may specify any number of queries, separated by a semi-colon.
Reload your page. And your page should be displayed as normal.
Please note:
Magento’s Setup Resources allow you to simply drop your install scripts onto the server, and have the system automatically run them. This allows you to have all your database migrations scripts stored in the system in a consistent format.
Using your favorite database client, take a look at the the core_setup table
This table contains a list of all the installed modules, along with the installed version number. You can see our module near the end
| yourmodule_setup | 0.1.0 |
This is how Magento knows not to re-run your script on the second, and on all successive, page loads. The yourmodule_setup is already installed, so it won’t be updated. If you want to re-run your installer script (useful when you’re developing), just delete the row for your module from this table.
Create upgrade script
So, that’s how you create a script that will setup your initial database tables, but what if you want to alter the structure of an existing module? Magento’s Setup Resources support a simple versioning scheme that will let you automatically run scripts to upgrade your modules.
Once Magento runs an installer script for a module, it will never run another installer for that module again (short of manually deleting the reference in the core_resource table). Instead, you’ll need to create an upgrade script. Upgrade scripts are very similar to installer scripts, with a few key differences.
To get started, we’ll create a script at the following location, with the following contents
File: app/code/local/YourCompany/YourModule/sql/yourmodule_setup/mysql4-upgrade-0.1.0-0.2.0.php
echo 'Testing our upgrade script (mysql4-upgrade-0.1.0-0.2.0.php) and halting execution to avoid updating the system version number <br />';
die();
Upgrade scripts are placed in the same folder as your installer script, but named slightly differently. First, and most obviously, the file name contains the word upgrade. Secondly, you’ll notice there are two version numbers, separated by a “-“. The first (0.1.0) is the module version that we’re upgrading from. The second (0.2.0) is the module version we’re upgrading to.
If we cleared our Magento cache and reloaded a page, our script wouldn’t run. We need to update the the version number in our module’s config.xml file to trigger the upgrade
<modules>
<YourCompany_YourModule>
<version>0.2.0</version>
</YourCompany_YourModule>
</modules>
With the new version number in place, we’ll need to clear our Magento cache and load any page in our Magento site. You should now see output from your upgrade script.
Ya, you are done with SQL setpup installer. enjoy
If you have any query please write me I will give my best.