I am creating an admin module where the user import's a .xls file in magento, then the .xls file can be executed and the data inside has to be imported into the products table.
This is the error I get:
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), query was: INSERT INTO `catalog_product_entity` (`entity_type_id`, `attribute_set_id`, `type_id`, `sku`, `created_at`, `updated_at`) VALUES (?, ?, ?, ?, '2016-09-30 11:43:55', '2016-09-30 11:43:55')
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), query was: INSERT INTO `catalog_product_entity` (`entity_type_id`, `attribute_set_id`, `type_id`, `sku`, `created_at`, `updated_at`) VALUES (?, ?, ?, ?, '2016-09-30 11:43:55', '2016-09-30 11:43:55')
And this is the code that creates the product in the controller:
foreach ($data as $value) {
$rand = rand(1, 9999);
try {
// $product = Mage::getModel('catalog/product');
$product = new Mage_Catalog_Model_Product();
$product->setAttributeSetId(9)
->setStoreId(Mage::app()->getStore()->getId())
->setTypeId('simple')
->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH)
->setTaxClassId(2)
->setCreatedAt(strtotime('now'))
->setSku($value[0] . $rand)
->setName($value[1])
->setDescription($value[2])
->setShortDescription($value[3])
->setWeight((int) substr($value[4], 0, -1))
->setPrice((float) substr($value[5], 1))
// ->setCost((float) substr($value[5], 1))
->setStockData(array(
'use_config_manage_stock' => 0,
'manage_stock' => 1,
'min_sale_qty' => 1,
'max_sale_qty' => (int) substr($value[6], 0, -1),
'is_in_stock' => 1,
'qty' => (int) substr($value[6], 0, -1)
))
// ->setColor((int) substr($value[7], 0, -1))
////////////////////////////////////// CUSTOM OPTIONS FOR TYPE
////////////////////////////////////// CUSTOM OPTIONS FOR GLASS
////////////////////////////////////// CUSTOM OPTIONS FOR ALCHOL
////////////////////////////////////// CUSTOM OPTIONS FOR GRAPES
////////////////////////////////////// CUSTOM OPTIONS FOR BOTTLE FORMAT
////////////////////////////////////// CUSTOM OPTIONS FOR BOTTLE FORMAT
// ->setCountryOfManufacture($value[14])
// ->setNewsFromDate(strtotime('now'))
// ->setNewsToDate(strtotime('now'))
->setStatus(1)
->setCategoryIds(array(2, 3))
->setWebsideIds(array(1));
$product->save();
var_dump($product);
die(0);
} catch (Exception $e) {
Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
}
}
$wineModel->setId($this->getRequest()->getParam('id'))
->setStatus(1)
->setExecuted_at(now())
->save();
}