ここで、すべての製品名の「example」を「test」に置き換えたいと思います。どうすればいいですか?
製品名がデータベースのどこに保存されているかわかりません。sql コマンドはどのように記述すればよいですか?
SQLコマンドが
update table_name set product_name = replace(value,"example","test")
Magento で変更するにはどうすればよいですか?
ここで、すべての製品名の「example」を「test」に置き換えたいと思います。どうすればいいですか?
製品名がデータベースのどこに保存されているかわかりません。sql コマンドはどのように記述すればよいですか?
SQLコマンドが
update table_name set product_name = replace(value,"example","test")
Magento で変更するにはどうすればよいですか?
一般に、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();
}
}
次のコードはすべての製品を更新します。名前の「example」のインスタンスを「test」に変更し、更新時間を無視できるようにする「saveAttribute」メソッドを使用します。このスクリプトは、非常に短い時間 (適切なサーバーでは 1 分未満) で 10,000 の問題を実行する可能性があります。
このファイルは、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');
}
Magento のデータベースを直接更新するのは難しいですが、実行可能です。EAVのことを処理する必要があります。
製品の entity_id と sku はこのテーブルに格納されます
catalog_product_entity
製品属性名はこれらのテーブルの 1 つに保存されますが、システムには他のテーブルがある場合があります。
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
したがって、これら 3 つのテーブルを結合する必要があります。これがどのように機能するかを示すエンティティ関係図とサンプル 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
;
Vicky が述べたように、ストレート SQL を使用してこれを行わないでください。Magento はすべてを十分に抽象化しているため、必要ありません。
製品名を更新する目的によっては、これを行うためにモジュール/コントローラーが必ずしも必要ではない場合があります。これを 1 回だけ行う場合 (または必要に応じて)、バックエンドの製品管理グリッドに移動し、更新する製品が表示されるようにフィルターを設定し、左上 (下) の [すべて選択] ボタンをクリックします。ページング コントロール)、右上の [アクション] 選択入力から [属性の更新] を選択します。その後、これらすべての製品の名前を一度に更新できます。