2014-02-25 84 views
0

创建导入脚本来管理相关产品和我用这样的代码:如何从Magento中删除所有相关的产品链接?

$product = Mage::getModel('catalog/product')->load($product_id); 
$related_data = array(); 
related_data[$linked_product_id]['position']=1; 
$product->setRelatedLinkData($related_data); 
$product->save(); 

而且我得到的错误是这样的:

SQLSTATE[23000]: Integrity constraint violation: 1452 
Cannot add or update a child row: a foreign key constraint fails 
(`netztech`.`catalog_product_link`, CONSTRAINT 
`FK_CAT_PRD_LNK_LNKED_PRD_ID_CAT_PRD_ENTT_ENTT_ID` 
FOREIGN KEY (`linked_product_id`) 
REFERENCES `catalog_product_entity` (`entity_id`) ON DELETE C) 

回答

1

如果你想删除从商店所有相关产品链接,该easist方法是这样的:

$connection = Mage::getSingleton('core/resource')->getConnection('core_write'); 
$sql = "DELETE FROM `catalog_product_link` WHERE `link_type_id`=1"; 
$connection->query($sql); 

当然首先检查你的catalog_product_link_type表,什么是你的链接ID相关产品。通常它是1,但首先检查好!

+0

如何为一个产品做到这一点? – gSorry

+1

只需像这样扩展sql:DELETE FROM'catalog_product_link' WHERE'link_type_id' = 1 AND'product_id' = YOUR_PRODUCT_ID – imso077