0

I'm currently trying to integrate magento into an ERP System using an self written middleware. I get this message:

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)

I don't really know where the problem lies. The result Object of a Product get and Product write look similar. I checked the web already for help, but couldn't find an solution for it. I also don't really know where to search, because the message on top is the only one I get.

Any help is appreceated.


Well the code itself is split up into multiple sections

but I'll try to show as much as possible

 $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();
Chris West
  • 741
  • 13
  • 36

2 Answers2

1

Please check are you using right Attribute set id with your code. Please share the code where you have written the code to update/save the product.

Saurabh
  • 356
  • 2
  • 10
1

The error is telling you exactly what the problem is. You are trying to save a product with an incorrect attribute set id. By that it means that the attribute set id you have set or not set in this case is not in the eav_attribute_set table. I think it's because if you create a new product you are not setting it. If you are updating an existing one you dont need to set it.

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;
}
Ashley Swatton
  • 2,104
  • 1
  • 18
  • 27
  • thanks it helped a lot. Didn't see that one in my tutorial ^^ – Chris West Jan 10 '14 at 13:38
  • No probs. Note that magento uses Innodb and as such all foreign keys are verified to ensure the integrity of the data. If you get more errors like this check the value being used as a foreign key and ensure it is in the database and valid. – Ashley Swatton Jan 10 '14 at 16:44
  • I'll have an close eye on this. Thanks a lot. – Chris West Jan 13 '14 at 12:43