2014-01-08 43 views
0

我目前正试图使用​​自行编写的中间件将magento集成到ERP系统中。 我收到此消息:magento产品创建/更新期间的完整性约束违规

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) 

我真的不知道问题出在哪里。 产品的结果对象get和Product写看起来类似。 我查过网页已经寻求帮助,但找不到解决方案。 我也不知道在哪里搜索,因为上面的消息是我唯一得到的。

任何帮助都附带了。


好代码本身是你使用权属性分成多个部分

,但我会尽量表现尽可能

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

回答

1

该错误告诉你到底是什么问题。您尝试使用不正确的属性设置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; 
} 
+0

感谢它帮了很多。在我的教程中没有看到那个^^ –

+0

没有probs。请注意,magento使用Innodb,因此所有外键都经过验证以确保数据的完整性。如果您收到更多此类错误,请检查用作外键的值,并确保它在数据库中并且有效。 – Ashley

+0

我会密切关注这一点。非常感谢。 –

1

请检查设置ID与你码。请分享您编写代码更新/保存产品的代码。

+0

更新了我的文章希望它有帮助 –

相关问题