Magento Installation Scripts – Part 7: Best Practices (Cool Ryan)

    Now that you have a good idea of what to do with installation files, here are some tidbits, nuggets, and lessons learned over the years of creating installation files for all of the modules I’ve come across.

    One Setup Per Module

    It is technically possible to setup multiple setups inside a single module.  Magento’s flexibility can sometimes be it’s downfall.  You can use any number of resource setup folders in a file, but it’s a best practice to use only one per module.

    Always Use The ORM

    In the earlier days of Magento, the installation scripts would just use the run()method and dump a query inside of it.  Many developers got stuck in that nasty habit.  Even after Magento started using their ORM for creating tables in later versions.  Developers are stuck in their ways and sometimes it can be hard to move forward. Using the run()method  to install tables is easier, but it essentially hard stamps the version of SQL you write it for into place permanently.  This prevents you from following Magento’s patches and upgrades to their ORM model.

    Make Your Installation Easy to Read

    It should be clear what the installation script is doing. Magento is known for heavy abstraction. Don’t do this in the installation script. Just because you can use a custom class for your installation script doesn’t mean you take the work out of the script and into the class. People have gotten use to the idea of looking for installation procedures in the script itself. Don’t get fancy by taking it out of the file.

    Separate Installations and Data Insertions

    Now that you know how and where to place data insertions, make sure to do it! I see people manipulate data all the time in their installation scripts. They remove attributes and update data from within the install files. Do it in the data install. Separate your responsibility.

    Use Installation Scripts Responsibly

    Do you need to refresh the indexes? Clear the cache? Insert 50,000 rows into a new table? Maybe the installation scripts aren’t the best place to do that.
    Not every store is built the same and some of these procedures can take hours to perform. If you need to reindex or refresh the cache, set the flag that says so instead. Don’t make the decision without the user’s consent.
    If you need to insert a lot of rows into your database, consider writing a Dataflow profile and letting the user insert these records themselves. The last thing you want is a user wondering why the page isn’t loading correctly and they stop the execution themselves. This effectively ruins the installation, your module, and your module’s reputation as well.

    Overview : Magento Installation Scripts (Cool Ryan) 
    Previous : Part 5: Attributes
    Next : Part 7: Best Practices

    Post from

    Recent Articles


    Related Stories

    Stay on op - Ge the daily news in your inbox