Setup Script throws Error : Call to undefined method Mage_Core_Model_Resource_Setup::addAttribute()

magento.stackexchange https://magento.stackexchange.com/questions/257992

  •  14-02-2021
  •  | 
  •  

Question

I am following a tutorial from the book of Roman Zenner & Vinai Kopp called "Magento - The Handbook for Developers" (german: Magento - Das Handbuch für Entwickler).

Snippet from config.xml

<config>
    <modules>
        <WR_EPO>
            <version>1.0.0.12</version>
        </WR_EPO>
    </modules>
    <global>
        <resources>
            <wr_epo_setup>
                <setup>
                    <module>WR_EPO</module>
                </setup>
            </wr_epo_setup>
            ...

It says that I need this setup script: The script is in (app/code/local/WR/EPO/sql/wr_epo_setup/upgrade-1.0.0.11-1.0.0.12):

<?php
/**
 * @var Mage_Sales_Model_Mysql4_Setup $installer
 */

$installer = $this;
$installer->startSetup();

Mage::Log("Setup Script is getting executed.", 7, "setup.log");

$installer->addAttribute('quote_item', 'shipping_surcharge', array(
        'label' => 'Versandkostenaufpreis'
        ,'type' => 'decimal'
    )
);

$installer->endSetup();

But I get a 500 Status Code Error and the error Call to undefined method Mage_Core_Model_Resource_Setup::addAttribute() is getting logged.


I also tried this:

Snippet from config.xml

<config>
    <modules>
        <WR_EPO>
            <version>1.0.0.12</version>
        </WR_EPO>
    </modules>
    <global>
        <resources>
            <wr_epo_setup>
                <setup>
                    <module>WR_EPO</module>
                </setup>
            </wr_epo_setup>
            <wr_epo_sales_setup>
                <setup>
                    <module>WR_EPO</module>
                    <class>Mage_Sales_Model_Mysql4_Setup</class>
                </setup>
            </wr_epo_sales_setup>
            ...

... and moved the setup script from app/code/local/WR/EPO/sql/wr_epo_setup/upgrade-1.0.0.11-1.0.0.12 to app/code/local/WR/EPO/sql/wr_epo_sales_setup/upgrade-1.0.0.11-1.0.0.12 but now the setup script is not getting executed at all, I already removed the entry in the core_resources table and reloaded the site.

Is the tutorial outdated? How to do it right?

Was it helpful?

Solution

The issue isn't with your setup script. You aren't using the proper setup resource in your config.xml.

You need to use the Mage_Sales_Model_Resource_Setup model, not the default setup model.

Example from Magento Sales module:

<config>
    <modules>
        <Mage_Sales>
            <version>1.0.0</version>
        </Mage_Sales>
    </modules>
    <global>
        <resources>
            <sales_setup>
                <setup>
                    <module>Mage_Sales</module>
                    <class>Mage_Sales_Model_Resource_Setup</class>
                </setup>
            </sales_setup>
        </resources>
    </global>
</config>
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top