【问题标题】:How can I update the product name with a sql query in Magento?如何在 Magento 中使用 sql 查询更新产品名称?
【发布时间】:2013-01-09 16:43:48
【问题描述】:

现在,我想将所有产品名称中的“示例”替换为“测试”。我该怎么做?

不知道产品名称存放在数据库的什么位置? sql命令应该怎么写?

我知道sql命令会是

 update table_name set product_name = replace(value,"example","test")

如何在 Magento 中更改它?

【问题讨论】:

  • 为什么要否决这个问题?

标签: magento magento-1.6


【解决方案1】:

通常,使用 sql 直接更新您的 magento 数据库是个坏主意。我建议您创建一个控制器,您可以在其中轻松运行这个小脚本:

public function databaseAction() {
    // load all products where 'name' LIKE '%example%'
    $products = Mage::getModel('catalog/product')->getCollection()
                ->addAttributeToSelect('name')
                ->addFieldToFilter(array(
                        array('attribute' => 'name', 'like' => '%example%')
                ));

    // loop through products, update the product name and save the product
    foreach ($products as $product) {
        $product->setName(str_replace('example', 'test', $product->getName()));
        $product->save();
    }
}

【讨论】:

  • 把代码放在哪里?谢谢。但是如果产品太多,代码会用完时间吗?
  • 您可以将代码放在您自己的自定义模块中。我建议您浏览this tutorial:,它解释了如何为其设置模块和控制器。我认为代码不会超时,除非您有大约 10000 种名称中带有“示例”的产品。如果您对此感到担心,可以将 ->setPageSize(100) 添加到集合负载中。这会将您的结果限制为 100 条记录或您输入的任何数量。
  • 使用这种方法,我相信你需要使用一个adminhtml控制器。
【解决方案2】:

以下代码将更新所有产品,它将名称中的任何“example”实例更改为“test”,并使用“saveAttribute”方法,这使得更新时间可以忽略不计。该脚本可能会在很短的时间内运行 10k 个问题,在体面的服务器上

此文件将放在 Magento 安装的子目录中。调用 $product->save() 会在后台发生一些额外的事情(例如触发事件......但对于您的目的可能不是必需的)。 save() 是一个更完整的方法,但耗时更长。

<?php
ini_set('error_reporting', E_ALL);
ini_set('display_errors', TRUE);
ini_set('log_errors', TRUE);
set_time_limit(0);

//Include the Mage package
require_once '../app/Mage.php';

//Set Developer mode to true, allows for more verbose errors
Mage::setIsDeveloperMode(true);

//Set php to display errors
ini_set('display_errors', 1);

//Initialize the app.
//Mage::app();
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

//Start timer
function microtime_float()
{
    list($usec, $sec) = explode(" ", microtime());

    return ((float) $usec + (float) $sec);
}

$time_start = microtime_float();
///////END HEADER//////

$products = Mage::getModel('catalog/product')->getCollection()->addAttributeToFilter('name',array('like'=>'%example%'));

foreach ($products as $product) {
    $newName = str_replace('example','test',$product->getName());
    $product->setName($newName);
    //can replace the next line with:  $product->save()
    $product->getResource()->saveAttribute($product,'name');
}

【讨论】:

    【解决方案3】:

    直接更新 Magento 的数据库很棘手但可行。需要处理 EAV 的事情。

    产品entity_id和sku都存放在这个表中

    catalog_product_entity
    

    产品属性名称存储在这些表之一中,您的系统中可能还有其他表。

    catalog_product_entity_datetime
    catalog_product_entity_decimal
    catalog_product_entity_gallery
    catalog_product_entity_int
    catalog_product_entity_media_gallery
    catalog_product_entity_text
    catalog_product_entity_url_key
    catalog_product_entity_varchar
    

    属性值存放在

    eav_attribute
    

    因此,您需要将这三个表连接在一起。这是一个实体关系图,展示了它的工作原理和示例 SQL。

    诀窍是知道要使用哪个entity_X 表,以及特定属性使用哪个attribute_id。 attribute_ids 因系统而异。此查询将帮助您查看您的查询。此示例仅查看 _entity_varchar 表。您的属性可能在 _entity_int 或 _entity_text 等中。此示例显示单个产品的值,在本例中 sku='0203MR-0'

    显示产品属性 ID

    select
       p.entity_id,
       p.entity_type_id,
       p.attribute_set_id,
       p.type_id,
       p.sku,
       a.attribute_id,
       a.frontend_label as attribute,
       av.value
    from
       catalog_product_entity p
       left join catalog_product_entity_varchar av on
          p.entity_id = av.entity_id
       left join eav_attribute a on
          av.attribute_id = a.attribute_id
    where
       p.sku = '0203MR-0'
    ;
    

    一旦您知道相关属性的attribute_id,您就可以列出或更新这些属性。在这种情况下,我们对产品名称感兴趣,而在我的系统中,attribute_id 是 71。

    列出产品名称

    select
       p.entity_id,
       p.entity_type_id,
       p.attribute_set_id,
       p.type_id,
       p.sku,
       a.attribute_id,
       a.frontend_label as attribute,
       av.value
    from
       catalog_product_entity p
       join catalog_product_entity_text av on
          p.entity_id = av.entity_id
       join eav_attribute a on
          av.attribute_id = a.attribute_id
    where
       a.attribute_id = 71
    ;
    

    更新产品名称

    update
       catalog_product_entity p
       join catalog_product_entity_text av on
          p.entity_id = av.entity_id
       join eav_attribute a on
          av.attribute_id = a.attribute_id
    set
       av.value = replace(av.value,
          'example',
          'test')
    where
       a.attribute_id = 71
    ;
    

    【讨论】:

      【解决方案4】:

      正如 Vicky 所说,不要使用直接的 SQL 来执行此操作。 Magento 将所有内容抽象得足够好,以至于没有必要。

      根据更新产品名称的目的,您可能不一定需要模块/控制器来执行此操作。如果您只是执行此操作一次(或偶尔执行此操作),只需转到后端产品管理网格,设置过滤器以便显示您要更新的产品,单击左上角的“全选”按钮(下方分页控件),然后从右上角的“操作”选择输入中选择“更新属性”。然后,您可以一次更新所有这些产品的名称。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-04-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多