7

I have adding product in front end.

I get below error message.

enter image description here

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)

public function saveVirtualNewProduct($wholedata)
{           
        $cats=array();
        foreach($wholedata['category'] as $keycat){
            array_push($cats,$keycat);
        }
        if($wholedata['status']==1 && isset($wholedata['wstoreids']) ){
            $status=1; 
            $stores=$wholedata['wstoreids'];
        }
        else{
            $status=Mage::getStoreConfig('marketplace/marketplace_options/product_approval')? 2:1;
            $stores=Mage::app()->getStore()->getStoreId();
        }
        $magentoProductModel = Mage::getModel('catalog/product');
        $magentoProductModel->setData($wholedata);
        $saved=$magentoProductModel->save();
        $lastInserId = $saved->getId();
/* Update  Special from-to Date */
    if($wholedata['special_from_date'] && $wholedata['special_to_date']) 
    {
        //Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
        $product = Mage::getModel('catalog/product')->load($lastInserId);
        $product->setSpecialFromDate($wholedata['special_from_date']);
        $product->setSpecialFromDateIsFormated(true);
        $product->setSpecialToDate($wholedata['special_to_date']);
        $product->setSpecialToDateIsFormated(true);
        $product->save();
    }
/* Update  Special from-to Date */

        $magentoProductModel = Mage::getModel('catalog/product')->load($saved->getId());
        $baseCurrencyCode = Mage::app()->getStore()->getBaseCurrencyCode();
        $currentCurrencyCode = Mage::app()->getStore()->getCurrentCurrencyCode();
        $allowedCurrencies = Mage::getModel('directory/currency')->getConfigAllowCurrencies(); 
        $rates = Mage::getModel('directory/currency')->getCurrencyRates($baseCurrencyCode, array_values($allowedCurrencies));

        if($wholedata['special_price']){
            $special_price = $wholedata['special_price']/$rates[$currentCurrencyCode];
            $magentoProductModel->setSpecialPrice($special_price);
        }

        $price = $wholedata['price']/$rates[$currentCurrencyCode];
        $magentoProductModel->setPrice($price);
        $magentoProductModel->setStoresIds(array($stores));
        $storeId = Mage::app()->getStore()->getId();
        $magentoProductModel->setWebsiteIds(array(Mage::getModel('core/store')->load( $storeId )->getWebsiteId()));
        $magentoProductModel->setCategoryIds($cats);
        $magentoProductModel->setStatus($status);
        $saved=$magentoProductModel->save();
        $lastId = $saved->getId();
        $this->_saveStock($lastId,$wholedata['stock'],$wholedata['is_in_stock']);

        $wholedata['id'] = $lastId;

        $collection1=Mage::getModel('marketplace/product');
        $collection1->setmageproductid($lastId);
        $collection1->setuserid($wholedata['userid']);
        $collection1->setstatus($status);
        $collection1->save();

        if($wholedata['type_id']=='virtual'){
        if((isset($_FILES) && count($_FILES) > 0) && !isset($wholedata['csv'])){
                if (!is_dir(Mage::getBaseDir().'/media/marketplace/')){
                mkdir(Mage::getBaseDir().'/media/marketplace/', 0755);
                }
                    if (!is_dir(Mage::getBaseDir().'/media/marketplace/'.$lastId)){
                    mkdir(Mage::getBaseDir().'/media/marketplace/'.$lastId, 0755);
                    }
                foreach($_FILES as $image){
                    $imagesdir = Mage::getBaseDir().'/media/marketplace/'.$lastId.'/';
                    $filename = $image['name'];                 
                    move_uploaded_file($image['tmp_name'],$imagesdir.$filename);    
                    }
                $this->_addImages($lastId,$wholedata['defaultimage']);
            }
        }
        return $lastId;     
    }

Please any one help me

Thanks.

  • do you have any setup scripts running? just the error message alone it will be hard to find out what is causing it. – David Manners Jul 19 '14 at 9:04
  • whats the code on line 18 in your MarketplaceaccountController.php? – MTM Jul 19 '14 at 9:29
  • Also a little suggestion, I guess you don't need to load the same product again and again to update. you can update and save using the model $magentoProductModel initialized at the start of the function. This will reduce the overhead. – MTM Jul 19 '14 at 9:35
  • 1
    Please check this great debugging tutorial and report back. – Fabian Blechschmidt Jul 19 '14 at 10:12
1

You are trying to create a product without the attribute_set_id or with a value for that column that does not exist in the attribute sets value.

and on a side note...you use too many save calls to save a single product.
Build one big array with attributes and assign it to the product model, then save only once.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.