Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/239.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将产品复制到所有网站-Magento multi store_Php_Mysql_Magento_Product - Fatal编程技术网

Php 将产品复制到所有网站-Magento multi store

Php 将产品复制到所有网站-Magento multi store,php,mysql,magento,product,Php,Mysql,Magento,Product,我正在尝试将我的产品从我创建的第一个网站复制到后来在多商店设置中添加的另外23个网站。 我有这样一个代码,我认为应该完成这项工作: $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

我正在尝试将我的产品从我创建的第一个网站复制到后来在多商店设置中添加的另外23个网站。 我有这样一个代码,我认为应该完成这项工作:

$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)
有人能告诉我为什么这个约束可能会失败吗? 为什么会在那里?我不想删除任何内容。
TIA

查看core_网站表。我猜数组中的一个或多个id无效(core_网站中没有匹配的网站id)。这将违反错误中指定的约束

catalog_product_网站表的完整约束为:

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_网站表中删除一个条目,则catalog_product_网站表中的所有关联条目(具有相同的网站id)也将被删除。这是一种确保没有多余无用数据的方法


您的示例中没有删除任何内容。MySQL只喜欢在失败时显示整个约束项。

这通常发生在找不到产品时,例如,没有这样的ID/SKU或参数为空,然后模型返回新的products对象。由于这是一个空模型,它需要您用诸如名称、价格等数据来填充它。但是从代码的外观来看,这不是您想要做的


因此,答案是首先检查
$value['entity\u id']
是否实际设置为某个值且不为空,然后检查
$\u product->getData()
$\u product->getId()
是否返回了某个值,如果没有,则意味着找不到该产品。

您找到了解决问题的方法吗?