2012-07-06 51 views
1

我试图将我创建的第一个网站的产品复制到另一个以后在多中心设置中添加的23个网站。 我有这样的代码,我认为应该做的工作:将产品复制到所有网站 - Magento多店

$arr_stores = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24); 
$store_from = 1; // first store id 

// get observer data and process each imported simple product - my products that need to be copied to  other websites 
    $_event = $observer->getEvent(); 
    $_adapter = $_event->getAdapter(); 

    foreach($_adapter->getNewSku() as $sku => $value) { 

     if($value['type_id'] == 'simple'){ 
      // load the next product - this loads product correctly 
      $_product = Mage::getModel('catalog/product')->setStoreId($store_from)->load($value['entity_id']); 

        // set the websites 
      $_product->setWebsiteIds($arr_stores); 
      $_product->save(); 

        // clear the var 
      unset($_product); 

     } 
    } 

但我收到此错误信息:

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`workoutlife`.`catalog_product_website`, CONSTRAINT `FK_CATALOG_PRODUCT_WEBSITE_WEBSITE_ID_CORE_WEBSITE_WEBSITE_ID` FOREIGN KEY (`website_id`) REFERENCES `core_website` (`website_id`) ON DELET) 

谁能告诉我,为什么这种限制可能会失败? 为什么ON DELET在那里?我不想删除任何东西。 TIA

+0

您是否找到解决问题的方法? – infinity 2014-03-03 22:54:13

回答

0

看看core_website表。我猜测数组中的一个或多个id无效(在core_website中没有匹配的website_id)。这会违反错误中指定的约束。

为catalog_product_website表完整约束是:

CONSTRAINT `FK_CATALOG_PRODUCT_WEBSITE_WEBSITE_ID_CORE_WEBSITE_WEBSITE_ID` FOREIGN KEY (`website_id`) REFERENCES `core_website` (`website_id`) ON DELETE CASCADE ON UPDATE CASCADE 

ON DELETE CASCADE意味着如果您删除从core_website表项时,所有的相关条目中catalog_product_website表 - 用同一website_id - - 也将被删除。这是确保没有无用数据剩余的一种方法。

您的示例中没有任何内容正在被删除。 MySQL只是在失败时显示整个约束条目。

0

这通常发生在无法找到产品时发生,例如,没有这样的ID/SKU或者参数是NULL,那么模型返回新的产品对象。既然这是空的模型,它需要你填写数据,如名称,价格等,但通过不希望你想要做的代码的外观。

因此,答案是首先检查$value['entity_id']实际上是设置的东西,而不是NULL和第二检查$_product->getData()$_product->getId()如果不是,这意味着该产品无法找到正在恢复的东西。

相关问题