17

I'd like to add an attribute to products, and I want to do this programmatically. So I added a mysql-install-0.1.0.php to a module of mine, and I added this (inspired from existing examples) :

<?php

$installer = $this;

$installer->startSetup();

$installer->addAttribute('catalog_product', 'collection', array(
        'type'              => 'varchar',
        'backend'           => '',
        'frontend'          => '',
        'label'             => 'Collection',
        'input'             => 'text',
        'class'             => '',
        'source'            => '',
        'global'            => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
        'visible'           => false,
        'required'          => false,
        'user_defined'      => false,
        'default'           => '',
        'searchable'        => false,
        'filterable'        => false,
        'comparable'        => false,
        'visible_on_front'  => false,
        'unique'            => false,
        'apply_to'          => '',
        'is_configurable'   => false
    ));

$installer->endSetup();

Syntax seems OK, but when it comes to execute this part of the code, here is the problem :

Fatal error: Call to undefined method Mage_Core_Model_Resource_Setup::addAttribute() in /home/frleq/Dev/projets/AVIP/WORKSPACE/avip_magento/app/code/local/Smile/Magentaho/sql/magentaho_setup/mysql4-install-0.1.0.php on line 7

Do you see what's wrong? Code isn't so complicated, and it is inspired from existing and worling ones...

Thank you

2 Answers 2

28

You are using the wrong setup class. Check your setup class declaration in config.xml. You are using Mage_Core_Model_Resource_Setup. Try Mage_Eav_Model_Entity_Setup instead.

4
  • 2
    That's right, I forgot to mention the Setup class in config.xml: <setup> <module>Company_Module</module> <class>Mage_Eav_Model_Entity_Setup</class> </setup> Thanks a lot Anders
    – frinux
    Commented Aug 12, 2010 at 7:03
  • 6
    Don't! Use Mage_Catalog_Model_Resource_Setup instead of Mage_Eav_Model_Entity_Setup regarding Products/Catalog attributes or their additional attributes won't be set (i.e. "is_searchable", "is_visible_on_front" etc.) because of the _prepareValues() method in the setup class.
    – nevvermind
    Commented Apr 25, 2012 at 14:59
  • 1
    What if you need to use both?
    – tread
    Commented Jul 29, 2016 at 8:58
  • 1
    @surfeur190, in your install script, use $setup = new Mage_Eav_Model_Entity_Setup('core_setup'); $setup->addAttribute(... Commented May 9, 2017 at 14:44
13

If you want to use product-related options (filterable, searchable etc.), you should use Mage_Catalog_Model_Resource_Eav_Mysql4_Setup class.

In latest versions of Magento you should use Mage_Catalog_Model_Resource_Setup class.

1
  • 1
    Best answer! To be able to set the product specific settings like visible_on_front, you'll need to use a Mage_Catalog_* setup class.
    – Jan Tomka
    Commented Oct 17, 2013 at 3:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.