6

I'm writing an installation script in an extension that adds some new attributes. I'm using the following. I'm following and old tutorial (http://blog.chapagain.com.np/magento-adding-attribute-from-mysql-setup-file/), but it's no longer up-to-date because Mage_Eav_Model_Entity_Setup::addAttribute does not include any of the "Frontend Properties" mapping, denoted by // X. Is it possible to set these properties with addAttribute()? If not, how should I go about installing a new attribute with frontend properties?

$setup->addAttribute('catalog_product', 'testatt', array(
    'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
    'input' => 'select',
    'type' => 'int',
    'backend' => '',    // backend_model
    'frontend' => '',   // fronted_model
    'label' => 'ATT NAME HERE',
    'class' => '',  ,
    'user_defined' => true,
    'required' => false,

    // Frontend Properties start here
    'visible' => true, // X
    'filterable' => true,    // X

);

On EE 1.13.

Update: See benmarks' answer!

3 Answers 3

18

You are using the incorrect setup class. Mage_Eav_Model_Entity_Setup has never had access to catalog-specific properties, which reside in the additional table (catalog_eav_attribute).

You either need to register catalog/setup as your module's setup resource or (more likely) instantiate it directly in your setup script:

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

In order to understand the mapping, refer to Mage_Catalog_Model_Resource_Setup::_prepareValues(). I (warning: self-promotion) recently posted a detailed dive of EAV attribute configuration at http://www.webguys.de/magento/eav-attribute-setup/.

1
  • Yes, you are right. Mage_Catalog_Model_Resource_Setup inherits from Mage_Eav_Model_Entity_Setup and has additional mapping that are merged in addAttribute()!
    – laketuna
    Jan 3, 2014 at 20:34
8

you can try below one it will work for your requirement. This is the syntax of create the attribute via installer script.

$installer = $this;
$installer->startSetup();
$installer->addAttribute('catalog_product', 'page_layout', array(
  'type'              => 'varchar',
  'backend'           => '',
  'frontend'          => '',
  'label'             => 'Page Layout',
  'input'             => 'select',
  'class'             => '',
  'source'            => 'catalog/product_attribute_source_layout',
  'global'            => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
  'visible'           => true,
  'required'          => false,
  'user_defined'      => false,
  'default'           => '',
  'searchable'        => false,
  'filterable'        => false,
  'comparable'        => false,
  'visible_on_front'  => false,
  'unique'            => false,
  'group'             => 'Design'
));

$installer->endSetup();
3
  • I've seen this example while I was searching for how to do this. This does not work. I don't believe that Mage_Core_Model_Resource_Setup ($this) has addAttributes() defined.
    – laketuna
    Jan 3, 2014 at 18:29
  • Because user_defined is false in your example, you also cannot delete this attribute in the admin.
    – David Tay
    Feb 20, 2015 at 4:11
  • Yes @DavidTay I agree with you
    – Keyul Shah
    Feb 20, 2015 at 4:35
0

This script will work if you add class "Mage_Eav_Model_Entity_Setup" in your module config.xml setup block.

Your Answer

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

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