如何在Magento中重命名属性代码?

时间:2011-03-04 19:21:24

标签: magento attributes magento-1.4 entity-attribute-value

我想将现有属性的代码重命名为其他内容。原因是因为300多个产品填写了属性字段,我不想因为我更改了属性的代码而不得不重新导入所有这些产品。

7 个答案:

答案 0 :(得分:30)

使用具有以下内容的升级脚本要容易得多:

$installer = $this;
$installer->startSetup();
$installer->updateAttribute('catalog_product', 'old_attribute_code', array('attribute_code' => 'new_attribute_code'));
$installer->endSetup();

答案 1 :(得分:28)

您可以在eav_attribute.attribute_code的mysql中编辑它。请务必事先进行备份,然后在System>Index Management中重新编制索引。

答案 2 :(得分:2)

  

使用具有以下内容的升级脚本要容易得多   为此:

$installer = $this;
$installer->startSetup();
$installer->updateAttribute('catalog_product', 'old_attribute_code', array('attribute_code' => 'new_attribute_code'));
$installer->endSetup();

您可以从简单文件中运行此操作,而不是完整安装脚本,只需替换

即可
$installer = $this;

require_once('./app/Mage.php');
Mage::app();

$installer  = Mage::getModel('eav/entity_setup', 'core_setup');
...

答案 3 :(得分:2)

如果您有权访问数据库,则可以运行此SQL命令

UPDATE eav_attribute
SET   attribute_code = 'your_NEW_attribute_code'
WHERE attribute_code = 'your_OLD_attribute_code'

如果不是很明显,则无论使用它的地方都必须编辑代码

答案 4 :(得分:1)

从以下脚本中获取灵感:

<?php
$write = Mage::getSingleton('core/resource')->getConnection('core_write');
$write->query("
  UPDATE eav_attribute val
  SET  val.attribute_code = "SET VALUE WHAT YOU WANT"
  WHERE  val.attribute_id = (
     SELECT attribute_id FROM eav_attribute eav
     WHERE eav.entity_type_id = 4
       AND eav.attribute_code = 'price'
    )
");

答案 5 :(得分:0)

编辑完attribute_code并重新编制索引后,您可能还会发现需要清除magento缓存,包括Flush Magento Cache和Flush Cache Storage - 或者rm -rf var/cache/*(请参阅下面的注意事项)。

Magento使用缓存来存储Mage::getSingleton('catalog/product')->loadByAttribute('sku',$sku);引用的查询以及可能的其他类似调用。像这样的查询:

SELECT 1 AS `status`, `e`.`entity_id`, `e`.`type_id`, `e`.`attribute_set_id`, `e`.`entity_id`, `e`.`attribute_set_id`, `e`.`type_id`, `e`.`cost`, `e`.`created_at`, `e`.`enable_googlecheckout`, `e`.`gift_message_available`, `e`.`has_options`, `e`.`image_label`, `e`.`is_recurring`, `e`.`links_exist`, `e`.`links_purchased_separately`, `e`.`links_title`, `e`.`manufacturer`, `e`.`manufacturer_value`, `e`.`name`, `e`.`news_from_date`, `e`.`news_to_date`, `e`.`price`, `e`.`price_type`, `e`.`price_view`, `e`.`recurring_profile`, `e`.`required_options`, `e`.`shipment_type`, `e`.`short_description`, `e`.`sku`, `e`.`sku_type`, `e`.`small_image`, `e`.`small_image_label`, `e`.`special_from_date`, `e`.`special_price`, `e`.`special_to_date`, `e`.`tax_class_id`, `e`.`thumbnail`, `e`.`thumbnail_label`, `e`.`updated_at`, `e`.`url_key`, `e`.`url_path`, `e`.`visibility`, `e`.`weight`, `e`.`weight_type`, `e`.`[CUSTOM_ATTRIBUTE_CODE]`, `e`.`[CUSTOM_ATTRIBUTE_CODE]` FROM `catalog_product_flat_1` AS `e` WHERE (e.sku = '[SKU]') LIMIT 1

有关Flush Magento Cache和Flush Cache Storage的更多信息,请点击此处: http://blog.nexcess.net/2011/05/21/clearing-the-cache-in-magento/

文章中的重要段落是

  

对于那些使用默认文件系统缓存和Magento的人来说,这似乎是微不足道的。你可以进入并手动“rm -rf var / cache / *”来清除缓存。但是那些使用备用缓存类型(xcache,memcached,apc,db,sqlite),“Flush Magento Cache”的人可能没有做你想做的事情。因此,当所有其他方法都失败并且您希望确保缓存完全清晰时,请务必单击“刷新缓存存储”。

     

在我告别之前,如果您使用其中一种共享存储缓存类型 - 例如使用相同memcached实例的两个不同应用程序 - 请点击“Flush Cache Storage”可能会删除该其他应用程序的缓存条目之前的一个警告好。这可能不是你想要的,所以请注意。

答案 6 :(得分:0)

只需在Magento 2.4上进行测试

/** @var \Magento\Customer\Setup\CustomerSetup $customerSetup */
$customerSetup = $this->customerSetupFactory->create(['setup' => $this->moduleDataSetup]);
$customerSetup->updateAttribute(
    Customer::ENTITY,
    'old_attribute_code',
    'attribute_code', // don't change this one
    'new_attribute_code'
);
相关问题