Magento Switchable Install Script Setup Class

Magento Switchable Install Script Setup Class

When doing some database upgrades in Magento, and in general, the best and the smartest way would be to create install or upgrade scripts to keep a persistent data structure. When a core setup is not enough and attributes have to be created or updated, either the customer or catalog product setup class has to be defined. Switching between those two classes is not solved very well in Magento and this little snippet will make the switching as easy as pie.

When creating an install or update scripts that do ‘simple’ database modifications, like creating new tables or modifying table columns, a class name inside the config file can be omitted, which means that the core resource setup is used. If it has to be dealt with EAV structure, which customer and product attributes are using, an appropriate class name has to be defined in the config file.

Setup Class

Instead of changing one of those two setup classes in the config file each time when dealing with EAV structure, a new class will be created that will be able to switch between those two classes. That class will simply return a proper object, whether the customer or catalog product setup is called. A setup node has to be defined in the config file that will contain a newly created class as a setup class, which is shown below.


The setup class extends Magento’s default setup resource class and has two methods defined, one that returns customer resource setup object and the other one that returns the catalog product resource setup object. In each of them a setup name node has to be set, which is “damir_setup” in this example.

class Inchoo_Damir_Model_Resource_Setup extends Mage_Core_Model_Resource_Setup
	public function getCatalogSetup()
		return new Mage_Catalog_Model_Resource_Setup('damir_setup');
	public function getCustomerSetup()
		return new Mage_Customer_Model_Resource_Setup('damir_setup');

Installer Script

From now on, when doing any ‘simple’ database modifications, the core resource setup is called with $this variable, as it’s been done before, but on EAV modifications an appropriate method has to be called, depending which attribute is being modified. getCatalogSetup() method returns the catalog resource setup object and getCustomerSetup() method returns the customer resource setup object. The examples are shown below:

$installer = $this->getCatalogSetup();
$installer->addAttribute('catalog_product', ‘my_attribute', array(…));


$installer = $this->getCustomerSetup();
$installer->addAttribute('customer', ‘my_attribute', array(…));


With this little code snippet each of the setup classes can be easily called and used with no additional config file changes. Also, each of the setup classes can be used in a single upgrade or install script. This may not be a Magento way of doing this, but for me this is an elegant and bulletproof solution on Magento updates… In the end, it’s still how Magento does that.

You made it all the way down here so you must have enjoyed this post! You may also like:

External database connection in Magento Damir Serfezi
Damir Serfezi, | 4

External database connection in Magento

Magento’s database layout and its EAV structure Mladen Lotar
Mladen Lotar, | 9

Magento’s database layout and its EAV structure

Magento MySQL database diagram Tomislav Bilic
, | 31

Magento MySQL database diagram


  1. Thanks Marko! I missed that way somehow.
    @nevvermind I agree with you, it’s a bit of an old code. I wrote that this is not a Magento way, but nothing’s wrong with using the new operator. For some things, like the lib folder, there’s no other way than using that way.

  2. Marko’s right.

    Mage::getResourceModel('catalog/setup', 'damir_setup');

    Using the “new” operator in Magento?! C’mon, Inchoo, c’mon…

  3. Hi Damir,
    nice post. Are there any reasons to avoid using simple

    $installer = Mage::getResourceModel('catalog/setup', 'catalog_setup');
    $installer = Mage::getResourceModel('customer/setup','customer_setup');

    when you need to switch on the fly? I’ve seen Magento using it here and here.


Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <blockquote cite=""> <code> <del datetime=""> <em> <s> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.

Tell us about your project

Drop us a line. We'd love to know more about your project.