Magento:使用模块安装脚本添加产品属性
我在Magento中制作了我的模块,但是它需要将附加属性分配给所有产品.我制作了安装脚本,但是不起作用:
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文件:
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>
这是怎么了?
首先,这不是有效的config.xml.设置类的配置如下:
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>
您也可以使用自己的安装程序类来代替Mage_Eav_Model_Entity_Setup,但是它应该继承Mage_Eav_Model_Entity_Setup,因此您可以使用addAttribute代替手工伪造SQL查询.
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.