Magento Installation Scripts – Part 2: Your First Script (Cool Ryan)

    So let’s create your first installation script for your module!
    Let’s put together a quick module declaration file, so Magento knows your module exists.

    Installation scripts are based off of the version numbers of modules, so we will need to set this inside your config.xml.

    Next, you need to tell Magento that you have setup files to run.  Magento looks for these in the <resources> section under the <global> node of yourconfig.xml.  This is the same area that defines core_read, core_setup, core_write, etc.

    Now that you’ve let Magento know that you want to run setup files, you now need to create some files in the area where Magento wants to look.
    The folder you would create would be:app/code/local/Namespace/Modulename/sql/modulename_setup/
    If you notice, the modulename_setup folder has the same name as the<modulename_setup>  node in the config.xml.  Coincidence?  Nope!  Magento does that on purpose.
    Now we have to create our first file.  We will have to follow the naming convention we discussed in Part 1.  So, our first file name would be install-1.0.0.php.  The version number is located in the <version> node in yourconfig.xml.

    So what is all of this in the file?  Let’s go through line by line:

    This is just for better readability when using the install file.  (If you don’t know what I mean, try using blocks and view files in magento.  That gets confusing real quick).  So what is the $this referencing? It is referencingMage_Core_Model_Resource_Setup.  You can tell it to specify your own class in the config.xml, but that’s for another lesson.

    This runs some basic queries such as disabling foreign key checks, etc.  This can also be overridden to do some additional actions such as checking for folders, etc.  Check out  Varien_Db_Adapter_Pdo_Mysql::startSetup()  to get an idea.

    Much the same as the startSetup()  method above.  It runs some basic queries for housekeeping.
    Now you are all setup to start running commands.  This includes creating database tables, etc.  For now, to ensure it works, let’s throw a quick log file in there, to make sure it ran.

    Check your var/log/system.log file and verify that the line did in fact write.
    Also, check your database under the core_resource table and look for the entry modulename_setup under the code column.  There should be entries ‘1.0.0’ under the version and data_version columns as well.
    If so, success!


    Overview : Magento Installation Scripts (Cool Ryan) 
    Previous : Part 1: Overview 
    Next : Part 3: Installing New Tables

    Post from

    Recent Articles


    Related Stories

    Stay on op - Ge the daily news in your inbox