我试图创建一个从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