简体   繁体   English

在Magento中创建产品的完整性约束违规

[英]Integrity constraint violation creating Product in Magento

i'm trying to Create a Product from magento Frontend, but when execute the php code i receive this error: 我正在尝试从magento Frontend创建一个产品,但是当执行php代码时,我收到此错误:

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)

I found here: Create a product from PHP - Magento that should be a problem with attribute set ID, but i tried force the ID from eav_attribute_set table, then with the function: 我在这里找到: 从PHP创建一个产品 - Magento应该是属性集ID的问题,但我尝试从eav_attribute_set表强制ID,然后使用函数:

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? 那么问题可能不是属性集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! 谢谢!

Please check if you have set a valid $attributeSetId , if Mage::getModel('catalog/config')->getAttributeSetId('catalog_product','Set_evento') successfully returned a valid ID. 如果Mage::getModel('catalog/config')->getAttributeSetId('catalog_product','Set_evento')成功返回了有效ID Mage::getModel('catalog/config')->getAttributeSetId('catalog_product','Set_evento')请检查是否设置了有效的$attributeSetId

I had the same error and setting a valid attribute set ID solved the problem for me. 我有同样的错误,并设置有效的属性集ID解决了我的问题。

Basically foreign key constraint errors are caused by you trying to enter a value into that column that does not exist in the referenced table. 基本上,外键约束错误是由于您尝试在引用的表中不存在的列中输入值而引起的。 In this case - I'd check to see whether the value you're trying to insert into your primary table is allowed in your secondary table. 在这种情况下 - 我会检查您在辅助表中是否允许您尝试插入主表的值。

I had the same issue while running an importer, i found that the problem was some of the products i was trying to update did not exist adding this code worked for me. 我在运行导入程序时遇到了同样的问题,我发现问题是我试图更新的一些产品不存在添加此代码对我有用。

$productid = Mage::getModel('catalog/product')
            ->getIdBySku(trim($sku));
        if(!$productid){
            Mage::Log('Failed to load product with Sku:'.$sku,null,'cron.log');
            return;
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM