我試圖創建一個從Magento的前端產品,而是創造產品時執行的PHP代碼我收到此錯誤:完整性約束違規在Magento
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`magento`.`catalog_product_entity`, CONSTRAINT `FK_CAT_PRD_ENTT_ATTR_SET_ID_EAV_ATTR_SET_ATTR_SET_ID` FOREIGN KEY (`attribute_set_id`) REFERENCES `eav_attribute_set` (`attribute_set_id`) ON DE)
我發現這裏:Create a product from PHP - Magento這應該是一個問題與屬性集的ID,但我試圖力的ID從表eav_attribute_set,然後用功能:
Mage::getModel('catalog/config')->getAttributeSetId('catalog_product','Set_evento')
而與此功能以上但與「默認」值。沒變化。所以也許問題不是屬性集ID?
這是我的代碼:
$product = Mage::getModel('catalog/product');
$product->setSku(time());
$product->setName("Evento senza nome");
$product->setDescription("123");
$product->setShortDescription("1234");
$product->setPrice(0.00);
$product->setTypeId('virtual');
$attributeSetId = Mage::getModel('catalog/config')->getAttributeSetId('catalog_product','Set_evento');
$product->setAttributeSetId($attributeSetId);
$product->setCategoryIds(array($cat_id));
$product->setVisibility(4); // catalog, search
$product->setStatus(1); // enabled
// assign product to the default website
$product->setWebsiteIds(array(Mage::app()->getStore(true)->getWebsite()->getId()));
// for stock
$stockData = $product->getStockData();
$stockData['qty'] = 1;
$stockData['is_in_stock'] = 1;
$product->setStockData($stockData);
$product->setCreatedAt(strtotime('now'));
Mage::app()->getStore()->setId(Mage_Core_Model_App::ADMIN_STORE_ID);
$product->save();
謝謝!
我試過,但我不覺得有什麼問題...... – user1781462
OK,讓你想進入'catalog_product_entity'列中的值 - 它在'eav_attribute_set'表中存在? – Swomble