Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Integrity constraint violation creating Product in Magento
 
leen15
Jr. Member
 
Total Posts:  2
Joined:  2012-10-03
 

i’m trying to Create a Product from magento Frontend, but when execute the php code i receive this error:

SQLSTATE[23000]Integrity constraint violation1452 Cannot add or update a child rowa foreign key constraint fails (`magento`.`catalog_product_entity`, CONSTRAINT `FK_CAT_PRD_ENTT_ATTR_SET_ID_EAV_ATTR_SET_ATTR_SET_IDFOREIGN KEY (`attribute_set_id`) REFERENCES `eav_attribute_set` (`attribute_set_id`) ON DE)

I found here that should be a problem with attribute set ID, but i tried force the ID from eav_attribute_set table, then with the function:

Mage::getModel('catalog/config')->getAttributeSetId('catalog_product','Set_evento')

And with this function above but with “Default” value. Nothing change. So maybe the problem is not Attribute Set ID?

This is my code:

$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();

Thanks!

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top