问题
Now, I want to replace in all product names 'example' with 'test'. How do I do this?
I don't know where the product name is stored in the database? How should I write the sql command?
I know the sql command will be
update table_name set product_name = replace(value,"example","test")
How do I change it in Magento?
回答1:
Generally, it's a bad idea to directly update your magento database using sql. I'd recommend you create a controller, where you can easily run this little script:
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();
}
}
回答2:
The following code will update all products, it will change any instance of "example" to "test" in the name and uses the "saveAttribute" method which makes the update time negligible. This script will likely run 10k problems in a very short time, < 1 minute on a decent server.
this file would go in a sub-directory of your Magento install. calling $product->save() has some additional things going on in the background (such as firing events...but may not be necessary for your purposes). save() is a more complete method, but takes longer.
<?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:
As stated by Vicky, don't do this using straight SQL. Magento abstracts everything well enough that it isn't necessary.
Depending on what the purpose is for updating the product names, you may not necessarily need a module/controller to do this. If you're just doing this once (or on occasion as necessary) just go the backend product management grid, set the filter so that the products you want to update are displayed, click the 'Select All' button on the upper left (below the paging controls), then choose 'Update Attributes' from the 'Actions' select input on the upper right. Then you can just update the name for all those products at once.
回答4:
Direct update of Magento's Database is tricky but doable. The EAV thing needs to be dealt with.
The product entity_id, and sku are stored in this table
catalog_product_entity
Product attribute names are stored in one of these tables, there may be others in your system.
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
The attribute values are stored in
eav_attribute
So you need to join these three tables together. Here is an Entity Relationship Diagram which shows how it works, and sample SQL.
The trick is to know which of the entity_X tables to use, and what attribute_id is used for the particular attribute. The attribute_ids will vary from system to system. This query will help you see yours. This example is only looking in the _entity_varchar table. Your attribute might be in _entity_int, or _entity_text, etc. This example show values for a single product, in this case where sku='0203MR-0'
Show product 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'
;
Once you know the attribute_id for the attribute in question, you can list or update those attributes. In this case we are interested in the Product Name, and in my system the attribute_id is 71.
List product names
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 product names
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
;
来源:https://stackoverflow.com/questions/14242066/how-can-i-update-the-product-name-with-a-sql-query-in-magento