Download the source for this entire series here!

Prepare the database

Next we’ll create the installation script that installs the twits table into the database.

  1. 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.php

    We simply included a sql folder that will house the eventual table creation sql code.

  2. Next up, updating the config.xml again. 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 > resources section that tells the setup script the folder for installation. We’ve also changed the version number to 0.2.0. When the page is initiated and Magento detects that the version has been upgraded, it will run the appropriate install script.

  3. 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();
  4. 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 of twit_setup in its core_resource table 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)
  5. Once again, wake up the Magento installer by logging into the admin backend, System > Configuration > Advanced. Click on Save Config. Clear your cache at /var/cache too just to be sure.
  6. Check to see if your table is created.
  7. Once your table is created, you can delete the mysql4-upgrade-0.1.0-0.2.0.php file. 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”

  1. Magento Developer Says:

    Great Article about magento custom extension development. Thank you for sharing it with us.

  2. gaweee Says:

    Glad i could help!

  3. JonUK Says:

    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!

  4. gaweee Says:

    make sure your mysql4-upgrade-0.1.0-0.2.0.php file begins with < ?php
    :D 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!

  5. JonUK Says:

    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?

  6. ColinV Says:

    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

  7. Christopher Shennan Says:

    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.

  8. Bernard Says:

    I thought no one was ever going to write about writing extensions for Magento!

    Thanks. Some really useful info here.

  9. tight Says:

    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”)

Leave a Reply