我正在使用php腳本創建新產品並更新Magento 1.7.0.2中的現有產品。 這個腳本可以很好地處理單個或幾百個產品。如果創建或更新大量的產品,說5000,得到以下錯誤Magento產品創建 - 違反完整性約束
SQLSTATE [23000]:完整性約束違規:1452不能添加或更新子行,外鍵約束失敗(magento_live
catalog_product_entity_varchar
,約束FK_CAT_PRD_ENTT_VCHR_ENTT_ID_CAT_PRD_ENTT_ENTT_ID
。外鍵(entity_id
)參考文獻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個產品後,仍會得到相同的錯誤。在運行該過程之前,我還將索引模式更改爲手動模式。
任何幫助將非常感激。謝謝!
我已經檢查過出現約束錯誤的產品,比如100個產品,創建產品不會給出任何錯誤。但是如果我嘗試創建10,000個產品,那麼它會在創建幾千個產品後給出約束違規錯誤。 –
我已經使用代碼創建了30k個產品,並且我只在類別ID的重複條目中遇到此錯誤。但直到現在還面臨着一個問題就是'記憶力枯竭問題'。假設如果你開始插入10k產品,但是在3k之後你的問題發生了,那麼你開始從3k插入到4k,如果問題再次出現,那麼我確定它只適用於重複輸入,否則不適用。 – monojit