我目前正在尝试使用自编写的中间件将magento集成到ERP系统中。 我收到这条消息:
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` (`a)
我真的不知道问题出在哪里。 Product get和Product write的结果看起来很相似。 我已经检查了网络上的帮助,但无法找到解决方案。 我也不知道在哪里搜索,因为顶部的消息是我得到的唯一消息。
任何帮助都会被激活。
代码本身分为多个部分
但我会尝试尽可能多地展示
$this->product = Mage::getModel('catalog/product')->loadByAttribute('sku',$this->handler['art_nr']);
if($this->product===false || $this->product->getId()<1){
$this->product = Mage::getModel('catalog/product');
$this->product->setSku($this->handler['art_nr']);
$this->newProduct = true;
}
$this->product->setStatus($this->shoparticle['products_status']);
$this->product->setName($this->handler['art_name']);
$categories = array();
if(!$this->isNewProduct()){
$categories = $this->product->getCategoryIds();
}
$categories = $this->handler['all_categories'];
$this->product->setCategoryIds($categories);
$crosssellingSet = array();
$upsellingSet = array();
$relatedSet = array();
if(is_array($this->handler['xselling']) && count($this->handler['xselling'])>0){
foreach($this->handler['xselling'] as $valueSet){
$product = Mage::getModel('catalog/product')->loadBySku($valueSet['art_nr']);
if((int)$valueSet['group']===1){
$crossselling[$product->getId()] = array('position'=>$valueSet['sort_oder']);
}else if((int)$valueSet['group']===2){
$upsellingSet[$product->getId()] = array('position'=>$valueSet['sort_oder']);
}else if((int)$valueSet['group']===3){
$relatedSet[$product->getId()] = array('position'=>$valueSet['sort_oder']);
}
}
}
$this->product->setCrossSellProductsData($crosssellingSet);
$this->product->setUpsellingProductsData($upsellingSet);
$this->product->setRelatedProductsData($relatedSet);
$importDir = Mage::getBaseDir('media') . DS . 'import' . DS;
//check if exists and add .htaccess file for protection
if(!is_dir($importDir)){
@mkdir($importDir,0775,true);
@chmod($importDir,0775);
}
if(!is_dir($importDir)){
throw new Connector_Model_Exception_Error('Could not create import Directory!');
}
if(!file_exists($importDir.'.htaccess')){
file_put_contents($importDir.'.htaccess','Order deny,allow'."\n".'Deny from all'."\n");
}
//clean direcotry
$dir = dir($importDir);
while(($e=$dir->read())!==false){
if(strpos($e,'.jpg')||strpos($e,'.png')||strpos($e,'.jepg')||strpos($e,'.gif')||strpos($e,'.tif')){
@unlink($importDir.$e);
}
}
//write images into directory
//and run Import
foreach($this->handler['images'] as $image){
file_put_contents($importDir.$image['image_name'],$image['image']);
$this->product->addImageToMediaGallery($importDir.$image['image_name'], array('image', 'small_image', 'thumbnail'), false, false);
}
$groups = Mage::getModel('customer/group')->getCollection()->getAllIds();
if((float)$this->handler['Bpreis'] > 0.00){
$this->product->setPrice((float)$this->handler['Bpreis']);
}
if((float)$this->handler['art']['products_pprices'] > 0.00){
$this->product->setMsrp((float)$this->handler['art']['products_pprices']);
}
//preapre the price data for ranges
$groupsets = array();
if(count($this->handler['PGROUP'])){
foreach($this->handler['PGROUP'] as $group){
if(in_array(((int)$group['gruppe']-250),$groups)){
$groupsets[((int)$group['gruppe']-250)][(float)$group['marge']] = (float)$group['PGPRICE'];
}
}
}
//Now run ageanst groupsets to set price range etc
$storeid = Mage::app()->getStore()->getWebsiteId();
foreach($groupsets as $groupid=>$rangeset){
if(count($rangeset)>0){
foreach($rangeset as $key=>$value){
if(count($rangeset)===1 && $key === (float)0){
$this->product->setData(
'group_price',
array(
'website_id'=>$storeid,
'cust_group'=>$groupid,
'price'=>$value,
)
);
}else{
$this->product->setData(
'tier_price',array(
'website_id'=>$storeid,
'cust_group'=>$groupid,
'price'=>$value,
'price_qty'=>$key
)
);
}
}
}
}
Mage::app()->getStore()->setId(Mage_Core_Model_App::ADMIN_STORE_ID);
if($this->isNewProduct()){
$this->product->setCreatedAt(strtotime('now'));
}
$this->product->save();
答案 0 :(得分:1)
请检查您是否在代码中使用了正确的属性集ID。请分享您编写代码的代码以更新/保存产品。
答案 1 :(得分:1)
错误正在告诉您问题的确切原因。您正在尝试使用不正确的属性集ID保存产品。这意味着在这种情况下您设置或未设置的属性集ID不在eav_attribute_set表中。我认为这是因为如果你创建一个新产品,你就不会设置它。如果您要更新现有的,则无需进行设置。
if($this->product===false || $this->product->getId()<1){
$this->product = Mage::getModel('catalog/product');
$this->product->setSku($this->handler['art_nr']);
// Set Attribute Set. Should be numeric for simple, bundle, configurable, grouped etc
$this->product->setAttributeSetId($this->handler['art_attribute_set_id']);
$this->newProduct = true;
}