如何从Magento中删除所有相关的产品链接?
我创建导入脚本来管理相关产品,并使用如下代码:如何从Magento中删除所有相关的产品链接?,magento,product,Magento,Product,我创建导入脚本来管理相关产品,并使用如下代码: $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]
$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)
$connection = Mage::getSingleton('core/resource')->getConnection('core_write');
$sql = "DELETE FROM `catalog_product_link` WHERE `link_type_id`=1";
$connection->query($sql);
我得到的错误如下:
$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)
$connection = Mage::getSingleton('core/resource')->getConnection('core_write');
$sql = "DELETE FROM `catalog_product_link` WHERE `link_type_id`=1";
$connection->query($sql);
如果要从商店中删除所有相关的产品链接,easist方法如下:
$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)
$connection = Mage::getSingleton('core/resource')->getConnection('core_write');
$sql = "DELETE FROM `catalog_product_link` WHERE `link_type_id`=1";
$connection->query($sql);
当然,首先在目录\产品\链接\类型表中检查相关产品的链接id。通常为1,但最好先检查 如何对一个产品执行此操作?只需像这样扩展sql:从
目录\u产品\u链接
中删除链接\u类型\u id
=1和产品\u id
=您的产品\u id