現在,我想用'test'替換所有產品名稱中的'example'。我該怎麼做呢?如何使用Magento中的SQL查詢更新產品名稱?
我不知道產品名稱存儲在數據庫中的什麼位置?我應該如何編寫sql命令?
我知道SQL命令將
update table_name set product_name = replace(value,"example","test")
如何改變在Magento?
現在,我想用'test'替換所有產品名稱中的'example'。我該怎麼做呢?如何使用Magento中的SQL查詢更新產品名稱?
我不知道產品名稱存儲在數據庫中的什麼位置?我應該如何編寫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();
}
}
在哪裏把你的代碼?謝謝。但如果產品太多,代碼是否會耗盡時間? – stack2013110
您可以將代碼放入您自己的自定義模塊中。我建議你通過[本教程:](http://alanstorm.com/magento_controller_hello_world)解釋如何爲它設置模塊和控制器。我不認爲在代碼中會有超時,除非你的名字中有大約10000個具有「示例」的產品。如果你擔心這一點,你可以添加' - > setPageSize(100)'到集合負載。這會將您的結果限制爲100條記錄或您輸入的任何數字。 – Vicky
使用這種方法,我相信你將需要使用adminhtml控制器。 – benmarks
如前所述由玉萍,不這樣做直使用SQL。 Magento將所有內容都抽象出來,以至於沒有必要。
根據更新產品名稱的目的,您可能不一定需要模塊/控制器來執行此操作。如果您只是一次(或根據需要)進行此操作,只需進入後端產品管理網格,設置過濾器以顯示要更新的產品,然後單擊左上角的「全選」按鈕(下方分頁控件),然後從右上角的「操作」選擇輸入中選擇「更新屬性」。然後,您可以立即更新所有這些產品的名稱。
以下代碼將更新所有產品,它會將名稱中的任何「example」實例更改爲「test」,並使用「saveAttribute」方法使更新時間忽略不計。這個腳本很可能會在很短的時間內運行10k個問題,在一個體面的服務器上運行1分鐘就會運行<。
這個文件將進入你的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
產品屬性名稱存儲在這些表中的一個,可能有其他人在你的系統。
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
所以,你需要這三個表連接在一起。這是一個Entity Relationship Diagram,它顯示了它是如何工作的,並示範了SQL。
訣竅是知道使用該entity_X表,以及用什麼樣attribute_id的特定屬性。 attribute_ids因系統而異。這個查詢會幫助你看到你的。這個例子只在_entity_varchar表中查找。你的屬性可能在_entity_int或_entity_text等。這個例子顯示了一個單一的產品價值,在這種情況下SKU =「0203MR-0」
顯示產品attribute_ids
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
;
爲什麼downvote的題? – stack2013110