Question

![1062 Duplicate entry][2] In Magento's Product creation code, I add my own custom code in /htdocs/magento/app/code/core/Mage/Adminhtml/controllers/Catalog/ProductController.php I have make external mysql connection & use that for inserting product id & sku in custom table.(I know this is not recommended way but just for testing purpose I am doing this.)

My code-:

public function saveAction()
{
        $storeId        = $this->getRequest()->getParam('store');
        $redirectBack   = $this->getRequest()->getParam('back', false);
        $productId      = $this->getRequest()->getParam('id');
        $isEdit         = (int)($this->getRequest()->getParam('id') != null);

        $data = $this->getRequest()->getPost();
        if ($data) {
            if (!isset($data['product']['stock_data']['use_config_manage_stock'])) {
                $data['product']['stock_data']['use_config_manage_stock'] = 0;
            }
            $product = $this->_initProductSave();

            try {
                $product->save();
                $productId = $product->getId();
//-------My code start---------------------------------
     $db_name = "magento";
             $con = mysql_connect  ("www.xyz.com", "magento", "password");
            If(!$con)
    {
        die('Could not connect: ' . mysql_error());
        mysql_close($con);
    }
    $seldb = mysql_select_db($db_name, $con);

    $query_fetch = "SELECT cpe.entity_id, cpe.sku FROM catalog_product_entity cpe
               WHERE cpe.entity_id = ".$productId;

    $result_query_fetch = mysql_query($query_fetch);
    while($row = mysql_fetch_array($result_query_fetch))
    {
        $entity_id = ($row["entity_id"]);
        $sku = ($row["sku"]);           
        $result_fetch = "$entity_id".",'".$sku."'";
    }

         $query_insert = "INSERT into product_creation(entity_id,sku,creation_date) VALUES(".$result_fetch.", NOW())";
    $result_insert = mysql_query($query_insert);

    mysql_close($con);
  //--------------------------My code End-----------------------------------------

                /**
                 * Do copying data to stores
                 */
                if (isset($data['copy_to_stores'])) {
                    foreach ($data['copy_to_stores'] as $storeTo=>$storeFrom) {
                        $newProduct = Mage::getModel('catalog/product')
                            ->setStoreId($storeFrom)
                            ->load($productId)
                            ->setStoreId($storeTo)
                            ->save();
                    }
                }


                Mage::getModel('catalogrule/rule')->applyAllRulesToProduct($productId);

                $this->_getSession()->addSuccess($this->__('The product has been saved.'));


            }
            catch (Mage_Core_Exception $e) {
                $this->_getSession()->addError($e->getMessage())
                    ->setProductData($data);
                $redirectBack = true;
            }
            catch (Exception $e) {
                Mage::logException($e);
                $this->_getSession()->addError($e->getMessage());
                $redirectBack = true;
            }
        }

        if ($redirectBack) {
            $this->_redirect('*/*/edit', array(
                'id'    => $productId,
                '_current'=>true
            ));
        }
        else if($this->getRequest()->getParam('popup')) {
            $this->_redirect('*/*/created', array(
                '_current'   => true,
                'id'         => $productId,
                'edit'       => $isEdit
            ));
        }
        else {
            $this->_redirect('*/*/', array('store'=>$storeId));
        }
    }

So from this code, product data is get inserted in both tables but I am getting error as

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '1949-1' for key 'IDX_STOCK_PRODUCT'

Can anybody plz help me resolve this issue...

Solution I tried-:

-remove /var/cache & /var/session

-clear browser cache & cookies

-In the app/etc/config.xml, change this

SET NAMES utf8 to this

SET NAMES utf8; SET FOREIGN_KEY_CHECKS=0; SET UNIQUE_CHECKS=0;

Still getting same error...Or plz tell me what should I change in my code?

P.S.- I know that this is not recommended way as I am changing code of core files & also using external connection rather than using ZEND connection...but it is just for testing purpose...

Is there any solution for this issue?

plz help me...

Était-ce utile?

La solution

The error is quite clear on what your problem is: You have a unique key constraint IDX_STOCK_PRODUCT and there already is a record with value '1949-1' in whatever column that key covers.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top