Iwo Kucharski
Iwo Kucharski

Reputation: 3825

Magento: Add product attributes with module installation script

I made my module in Magento, but it needs additional attribute assigned to all products. I made installation script, but it doesn't work:

<?php

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

$installer->run("
    INSERT IGNORE INTO `{$installer->getTable('eav/attribute')}` (`attribute_id`, `entity_type_id`, `attribute_code`, `attribute_model`, `backend_model`, `backend_type`, `backend_table`, `frontend_model`, `frontend_input`, `frontend_label`, `frontend_class`, `source_model`, `is_required`, `is_user_defined`, `default_value`, `is_unique`, `note`) VALUES
    SELECT 1 + coalesce((SELECT max(attribute_id) FROM `{$installer->getTable('eav/attribute')}`),0),4,`is_accepted`,NULL,NULL,`int`,NULL,NULL,`boolean`,`Accepted`,NULL,`eav/entity_attribute_source_boolean`,1,1,'0',0,NULL);
");

$installer->endSetup();

Config.xml file:

<adminhtml>
<acl>
    <module_setup>
                    <setup>
                        <module>My_module</module>
                    </setup>
                    <connection>
                        <use>core_setup</use>
                    </connection>
                </module_setup>

                <module_write>
                    <connection>
                        <use>core_write</use>
                    </connection>
                </module_write>

                <module_read>
                    <connection>
                        <use>core_read</use>
                    </connection>
                </module_read>
            </resources>
        </acl>
</adminhtml>

What is wrong here?

Upvotes: 1

Views: 4729

Answers (2)

Fabian Schmengler
Fabian Schmengler

Reputation: 24551

First, this is not a valid config.xml. The setup class is configured as follows:

<config>
    ...
    <global>
        ...
        <resources>
            ...
            <your_module_setup>
                <setup>
                    <module>Your_Module</module>
                    <class>Mage_Eav_Model_Entity_Setup</class>
                </setup>
            </your_module_setup>
            ...
        </resources>
        ...
    </global>
    ...
</config>

instead of Mage_Eav_Model_Entity_Setup you could also use your own setup class but it should inherit Mage_Eav_Model_Entity_Setup, so you can use addAttribute instead of forging the SQL queries by hand.

Then your setup script should look similar to this:

$installer = $this;
$installer->startSetup();
/*
 * adds product unit attribute to product
 */
$installer->addAttribute(Mage_Catalog_Model_Product::ENTITY, 'productunit_id', array(
    'label' => Mage::helper('productunits')->__('Quantity Unit'),
    'type' => 'int',
    'input' => 'select',
    'source' => SGH_ProductUnits_Model_Entity_Attribute_Source_Units::MODEL,
    'backend' => SGH_ProductUnits_Model_Entity_Attribute_Backend_Units::MODEL,
    'required' => 1,
    'global' => 1,
    'note' => Mage::helper('productunits')->__('This will be displayed next to any Qty value.')
));
$installer->endSetup();

It's my code that adds a quantity unit attribute, don't be confused by the use of class constants, those are just the corresponding class aliases.

Upvotes: 6

Tim Bezhashvyly
Tim Bezhashvyly

Reputation: 9100

Your <module_setup> node have to be under config/global/resources and not under config/adminhtml/acl.

Upvotes: 3

Related Questions