我正在使用 php 脚本来创建新产品并更新 Magento 1.7.0.2 中的现有产品。该脚本适用于单个或几百个产品。如果创建或更新大量产品,例如 5000 ,则会出现以下错误
SQLSTATE [23000]:完整性约束违规:1452 无法添加或更新子行:外键约束失败(magento_live
. catalog_product_entity_varchar
, CONSTRAINT FK_CAT_PRD_ENTT_VCHR_ENTT_ID_CAT_PRD_ENTT_ENTT_ID
FOREIGN KEY ( entity_id
) REFERENCES catalog_product_entity
( entity_id
) ON D)
我使用的脚本,
const DEFAULT_STORE_ID=0;
const DEFAULT_VISIBILITY=4;
const DEFAULT_TAXCLASS=2;
/**
* @var Mage_Catalog_Model_Product
*/
protected $magentoProduct;
protected $errors=array();
public function __construct()
{
Mage::app()->setCurrentStore(0);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
Mage::getSingleton('core/session', array('name' => 'adminhtml'));
$userModel=Mage::getModel('admin/user');
$userModel->setUserId(1);
$session=Mage::getSingleton('admin/session');
$session->setUser($userModel);
$session->setAcl(Mage::getResourceModel('admin/acl')->loadAcl());
}
public function getErrors()
{
return $this->errors;
}
public function save($productData)
{
$start=microtime(true);
$this->magentoProduct=Mage::getModel('catalog/product');
$magentoProductId=$this->magentoProduct->getIdBySku($productData['sku']);
$this->magentoProduct->load($magentoProductId);
if (!$this->magentoProduct->getId())
{
mage::log("ADDING sku-" . $productData['sku']);
try
{
$this->magentoProduct->setWebsiteIds(array(1));
$this->magentoProduct->setAttributeSetId($this->magentoProduct->getDefaultAttributeSetId());
$this->magentoProduct->setStoreId(self::DEFAULT_STORE_ID);
$this->magentoProduct->setTypeId('simple');
$this->magentoProduct->setSku($productData['sku']);
$this->magentoProduct->setName($productData['name']);
$this->magentoProduct->setDescription($productData['description']);
$this->magentoProduct->setShortDescription($productData['short_description']);
$this->magentoProduct->setPrice(floatval($productData['price']));
$this->magentoProduct->setCost(floatval($productData['cost']));
$this->magentoProduct->setIsImported($productData['is_imported']);
$this->magentoProduct->setMfgpartno($productData['mfgpartno']);
$this->magentoProduct->setEtilizeProductId($productData['etilize_product_id']);
$this->magentoProduct->setCondition($productData['condition']);
$this->magentoProduct->setManufacturer($productData['manufacture']);
$this->magentoProduct->setCategoryIds($productData['category_id']); // need to look these up
$this->magentoProduct->setWeight($productData['weight']);
$this->magentoProduct->setTaxClassId(self::DEFAULT_TAXCLASS); // taxable goods
$this->magentoProduct->setVisibility(self::DEFAULT_VISIBILITY); // catalog, search
$this->magentoProduct->setStatus(intval($productData['status'])); // enabled
$this->magentoProduct->setCategoryIds($productData['category_id']);
$this->magentoProduct->save();
Mage::log("saving stock for new product");
$stockItem = Mage::getModel('cataloginventory/stock_item');
$stockItem->assignProduct($this->magentoProduct);
$qty = (integer)$productData['qty'];
$stockItem->setData('qty', $qty);
$stockItem->setData('is_in_stock', $qty > 0 ? 1:0);
$stockItem->setData('stock_id', $productData['stock_id']);
$stockItem->setData('manage_stock', 0);
$stockItem->save();
}
catch (Exception $e)
{
Mage::log("Failed Saving Product: " . $e->getMessage());
}
$magentoProductId=$this->magentoProduct->getId();
unset($this->magentoProduct);
}
else
{
mage::log("UPDATING sku-" . $productData['sku']);
try
{
$this->magentoProduct->setMfgpartno($productData['mfgpartno']);
$this->magentoProduct->setEtilizeProductId($productData['etilize_product_id']);
$this->magentoProduct->setCondition($productData['condition']);
$this->magentoProduct->setPrice(floatval($productData['price']));
$this->magentoProduct->setCost(floatval($productData['cost']));
$this->magentoProduct->setWeight($productData['weight']);
$this->magentoProduct->setTaxClassId($productData['tax_class_id']);
$this->magentoProduct->setManufacturer($productData['manufacture']);
$qty=(integer)$productData['qty'];
$inStock=($qty > 0) ? 1 : 0;
$stockData = $this->magentoProduct->getStockData();
$stockData['qty'] = $qty;
$stockData['is_in_stock'] = $inStock;
$stockData['manage_stock'] = 1;
$stockData['use_config_manage_stock'] = 0;
$this->magentoProduct->setStockData($stockData);
$this->magentoProduct->save();
}
catch (Exception $e)
{
Mage::log("Failed Update Product: " . $e->getMessage());
}
$magentoProductId=$this->magentoProduct->getId();
unset($this->magentoProduct);
}
return $magentoProductId;
}
该脚本放置在 Magneto 自定义模块中,并且我在数据库表中有产品列表。我正在从该表中选择产品并使用上述脚本更新或插入 magento。该过程正在通过 curl 请求进行初始化。我还尝试将其插入 100 或 50 个批次中。在更新或插入大约 3000 个产品后仍然出现相同的错误。在运行该过程之前,我还将索引模式更改为手动。
任何帮助将非常感激。谢谢!