Magento产品创建错误... SQLSTATE [23000]:完整性约束违规:1062重复输入?

时间:2012-04-14 12:25:18

标签: magento constraints product mysql-error-1062 duplicates

![1062重复录入] [2] 在Magento的产品创建代码中,我在 /htdocs/magento/app/code/core/Mage/Adminhtml/controllers/Catalog/ProductController.php 中添加了我自己的自定义代码。我已经创建了外部mysql连接&用于插入产品ID& sku在自定义表中。(我知道这不是推荐的方法,但仅仅是为了测试目的我正在这样做。)

我的代码 - :

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));
        }
    }

因此,从这段代码中,产品数据会插入到两个表中,但我得到错误

SQLSTATE [23000]:完整性约束违规:1062密钥'IDX_STOCK_PRODUCT'的重复条目'1949-1'

任何人都可以帮我解决这个问题...

我尝试过的解决方案 -

-remove / var / cache&的/ var /会话

-clear浏览器缓存&饼干

- 在app / etc / config.xml中,更改此

SET NAMES utf8 到这个

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

仍然得到同样的错误......或者告诉我在代码中应该更改什么?

P.S.-我知道这不是推荐方式,因为我正在更改核心文件的代码和也使用外部连接而不是使用ZEND连接...但它仅用于测试目的......

这个问题有解决方法吗?

请帮助我...

1 个答案:

答案 0 :(得分:1)

错误很清楚你的问题是什么: 您有一个唯一的密钥约束IDX_STOCK_PRODUCT,并且已经有一个值为' 1949-1'在密钥所涵盖的任何列中。