This entry was posted on Tuesday, March 31st, 2009 at 4:12 am and is filed under development, howto. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.
Mar 31, 2009
Howto: Repackageable custom extension development in Magento – Part 3 – Database
Author: gaweee | Filed under: development, howtoPrepare the database
Next we’ll create the installation script that installs the twits table into the database.
- Once again, we begin with updating the file structure
app/ etc/ modules/ - SavantDegrees_All.xml (Or what ever your company name might be) code/ local/ SavantDegrees/ (Or what ever your company name might be) Twits/ (Or whatever your module name might be) Block/ - HelloWorld.php controllers/ - AdminController.php - IndexController.php etc/ - config.xml Helper/ - Data.php sql/ twits_setup/ - mysql4-install-0.2.0.php - mysql4-upgrade-0.1.0-0.2.0.phpWe simply included a
sqlfolder that will house the eventual table creation sql code. - Next up, updating the
config.xmlagain. This time to add the setup declarations.<?xml version="1.0"?> <config> <modules> <SavantDegrees_Twits> <version>0.2.0</version> </SavantDegrees_Twits> </modules> <global> <blocks> <twits> <class>SavantDegrees_Twits_Block</class> </twits> </blocks> <helpers> <twits><class>SavantDegrees_Twits_Helper</class></twits> </helpers> <resources> <twits_setup> <setup> <module>SavantDegrees_Twits</module> </setup> <connection> <use>core_setup</use> </connection> </twits_setup> <twits_write> <connection> <use>core_write</use> </connection> </twits_write> <twits_read> <connection> <use>core_read</use> </connection> </twits_read> </resources> </global> <adminhtml> <menu> <twits translate="title" module="twits"> <title>Twits</title> <sort_order>100</sort_order> <action>twits/admin</action> </twits> </menu> </adminhtml> <frontend> <routers> <SavantDegrees_Twits> <use>standard</use> <args> <module>SavantDegrees_Twits</module> <frontName>twits</frontName> </args> </SavantDegrees_Twits> </routers> </frontend> </config>
Here we’ve added a
global > resourcessection that tells the setup script the folder for installation. We’ve also changed the version number to0.2.0. When the page is initiated and Magento detects that the version has been upgraded, it will run the appropriate install script. - Lastly the install script itself:
mysql4-install-0.2.0.php<?php $installer = $this; $installer->startSetup(); $installer->run(" CREATE TABLE {$this->getTable('tips')} ( `tip_id` int(10) unsigned NOT NULL auto_increment, `title` varchar(250) NOT NULL default '', `author` varchar(250) default NULL, `contents` text, PRIMARY KEY (`tip_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; "); $installer->endSetup();
- Copy the same file contents to
mysql4-upgrade-0.1.0-0.2.0.php. Why? because the code above is correct. But since this HOWTO is going through the phases in version 0.1, 0.2, and etc. Magento has already a record oftwit_setupin itscore_resourcetable bound to the previous version of 0.1 (its first run). So what we need now is a upgrading script instead of an installer. But if/when you do package your extension, you shouldn’t need the upgrade code. Just install straight to version 0.2 (or higher) - Once again, wake up the Magento installer by logging into the
adminbackend,System > Configuration > Advanced. Click onSave Config. Clear your cache at/var/cachetoo just to be sure. - Check to see if your table is created.
- Once your table is created, you can delete the
mysql4-upgrade-0.1.0-0.2.0.phpfile. As explained above, you dont need it anymore!
» More Intangibles! … Part 4 – Model
9 Responses to “Howto: Repackageable custom extension development in Magento – Part 3 – Database”
Leave a Reply
Most Popular
- HOWTO: PHP and jQuery upload progress bar (48)
- JQuery Progress Bar 1.1 (42)
- Howto: Repackageable custom extension development in Magento - Part 2 - Admin Controller (24)
- Howto: Repackageable custom extension development in Magento - Part 8 - CRUD - Update (16)
- HOWTO: struts 2 i18n (13)
- JQuery Progress Bar 2.0 (12)
- JQuery Progress Bar 1.2 (11)
- Howto: Repackageable custom extension development in Magento (9)
- Howto: Repackageable custom extension development in Magento - Part 3 - Database (9)
- Howto: Repackageable custom extension development in Magento - Part 9 - Frontend - List (8)
Recent Comments
- learning methods: All the posts you talk
- best travel agency: Wow! I really enjoyed this
- Muzafar Ali: Hi, thanks for sharing a beautiful
- andy65007: After the progress bar
- Mark: Excellent tutorials. Most "how to
- Mark: As Jim said before, you
- Jeremy Roberts: I tried and it works
- Silas Nordes: Came to this site by
- 150cc mopeds: That was a superb blog
- Jason: if the situation is like
Latest Entries
- jQuery Progress Bar Configuration
- Extracting email addresses from inbox
- 10 Good (Free and Legal) Source for Photos and Images
- Howto: Backup Microsoft SQL Server Database, as in Dump it to a SQL Script (like MYSQL's sqldump)
- Managing client's expectation with wireframe software
- Howto: Repackageable custom extension development in Magento - Part 9 - Frontend - List
- JQuery Progress Bar 2.0
- HOWTO: Find icons for your new prototype system
- Google Maps Helper
- laying the cornerstones

November 4th, 2009 at 6:09 am
Great Article about magento custom extension development. Thank you for sharing it with us.
June 17th, 2009 at 1:29 pm
Glad i could help!
June 17th, 2009 at 9:07 am
Thanks Gary. Seems despite all efforts, a good tutorial can’t stop a developer being a twit. I omitted to update the version in config.xml to 0.2.0. Now works perfectly!
June 16th, 2009 at 1:28 pm
make sure your mysql4-upgrade-0.1.0-0.2.0.php file begins with < ?php
i didnt bother to write it in there but you need it for php instructions remember?
Also, if it still doesnt work
go into your core_resources table and change your version back to 0.1.0 and then go to to your admin page again, that’ll trigger the update script. I’ve just tested the scripts, it works just fine.
Good luck!
June 16th, 2009 at 12:53 pm
Great Article!
Using Magento 1.3.2, come hell or high water I’ve been unable to execute the upgrade script. core_resource still says 0.1.0 – any suggestions?
May 25th, 2009 at 3:05 pm
Absolutely fantastic article series. Thank you so much for publishing it. I feel like I have a much better handle on Magento now. One thing I wanted to mention was that for me, on 1.2.1.2, in config.xml, I had to use:
Twits
100
adminhtml/twit
instead of
Twits
100
admin/twit
Thanks again, and keep up the fantastic work!
-Colin
May 20th, 2009 at 6:59 am
This is a great article. The detailed information helped me solve my problem where I couldn’t get my blocks to display when there were located in the /app/code/local directory but they would display fine from the /app/code/core.
After a day of trying I really appreciate the detail included within this post and the fact it isn’t just a repeat of all the other basic “override a core block” blog entries.
Many Thanks.
May 18th, 2009 at 3:08 am
I thought no one was ever going to write about writing extensions for Magento!
Thanks. Some really useful info here.
April 21st, 2009 at 9:26 am
Using magento 1.3.1, the created table’s name was “twit_id” (instead of “twit”, problems with next part).
To avoid this problem, just skip the table creation (step 5 to 7).
If it’s to late, you can copy the mysql-upgrade script to “mysql4-upgrade-0.2.0-0.3.0.php” and change version in “config.xml” at the end of next part (reproduce step 5 to 7, table name will be bound to “twit”)