![1062 Duplicate entry][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'
谁能帮我解决这个问题...
我试过的解决方案-:
-删除 /var/cache 和 /var/session
-清除浏览器缓存和cookies
- 在 app/etc/config.xml 中,更改此项
设置名称 utf8 到这个
设置名称 utf8; 设置 FOREIGN_KEY_CHECKS=0;SET UNIQUE_CHECKS=0;
仍然得到同样的错误......或者请告诉我我应该在我的代码中改变什么?
PS-我知道这不是推荐的方式,因为我正在更改核心文件的代码并且还使用外部连接而不是使用 ZEND 连接......但这只是为了测试目的......
这个问题有什么解决办法吗?
请帮帮我...