1

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?

2 Answers 2

6

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Good idea, but still get an error, there's no method addAttribute() in setup: Fatal error: Call to undefined method Mage_Core_Model_Resource_Setup::addAttribute() Edit: Sorry, I forgot you wrote 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.
Ok, I added <class>Mage_Eav_Model_Entity_Setup</class> in my config.xml file and it works. Thanks! :)
3

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

2 Comments

Ok, you are right. But now i have error: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT max(attribute_id)+1 FROM eav_attribute),4,is_accepted,NULL,NULL,int' at line 2
please post a separate question if your original question has been answered. StackOverflow doesn't work like a forum with never-ending threads, there are separate posts for each question.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.