【问题标题】:Magento: Add product attributes with module installation scriptMagento:使用模块安装脚本添加产品属性
【发布时间】:2012-07-05 08:12:11
【问题描述】:

我在 Magento 中制作了我的模块,但它需要为所有产品分配额外的属性。我制作了安装脚本,但它不起作用:

<?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 文件:

<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>

这里有什么问题?

【问题讨论】:

    标签: mysql magento


    【解决方案1】:

    首先,这不是一个有效的 config.xml。 setup类配置如下:

    <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>
    

    您也可以使用自己的设置类代替 Mage_Eav_Model_Entity_Setup,但它应该继承 Mage_Eav_Model_Entity_Setup,因此您可以使用 addAttribute 而不是手动伪造 SQL 查询。

    那么您的安装脚本应该类似于:

    $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();
    

    是我的代码添加了数量单位属性,不要被类常量的使用弄糊涂了,那些只是对应的类别名。

    【讨论】:

    • 好主意,但仍然出现错误,设置中没有方法 addAttribute():Fatal error: Call to undefined method Mage_Core_Model_Resource_Setup::addAttribute() 编辑:对不起,我忘了你写的是 而不是 Mage_Eav_Model_Entity_Setup 你也可以使用你自己的设置类,但它应该继承 Mage_Eav_Model_Entity_Setup,因此您可以使用 addAttribute 而不是手动伪造 SQL 查询。
    • 好的,我在我的 config.xml 文件中添加了&lt;class&gt;Mage_Eav_Model_Entity_Setup&lt;/class&gt;,它可以工作了。谢谢! :)
    【解决方案2】:

    您的&lt;module_setup&gt; 节点必须在config/global/resources 下,而不是在config/adminhtml/acl 下。

    【讨论】:

    • 好吧,你是对的。但现在我有错误: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
    • 如果您的原始问题已得到解答,请发布单独的问题。 StackOverflow 不像论坛那样有永无止境的话题,每个问题都有单独的帖子。
    猜你喜欢
    • 1970-01-01
    • 2014-04-19
    • 1970-01-01
    • 2012-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-29
    相关资源
    最近更新 更多