我有一个与在 Magento 中创建产品的完整性约束违规类似的问题(未回答),但我正在创建一个自定义观察者,该观察者挂钩到 catalog_product_save_after 事件 - 基于本教程: http: //fishpig.co.uk/blog/custom-tabs -magento-product-admin.html
但是,每当保存新产品时,我都会收到此错误:
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '22-1' for key 'UNQ_CATALOGINVENTORY_STOCK_ITEM_PRODUCT_ID_STOCK_ID'
config.xml 如下所示:
<adminhtml>
<events>
<catalog_product_save_after>
<observers>
<a1web_save_product_data>
<type>singleton</type>
<class>metricimperial/observer</class>
<method>saveProductData</method>
</a1web_save_product_data>
</observers>
</catalog_product_save_after>
</events>
</adminhtml>
类的大纲是这样的:
<?php
class A1web_MetricImperialConverter_Model_Observer
{
/**
* Flag to stop observer executing more than once
*
* @var static bool
*/
static protected $_singletonFlag = false;
* @param Varien_Event_Observer $observer
*/
public function saveProductData(Varien_Event_Observer $observer)
{
if (!self::$_singletonFlag) {
self::$_singletonFlag = true;
$product = $observer->getEvent()->getProduct();
//Custom updates made to product object here
$product->save();
}
catch (Exception $e) {
Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
}
}
}
/**
* Retrieve the product model
*
* @return Mage_Catalog_Model_Product $product
*/
public function getProduct()
{
return Mage::registry('product');
}
/**
* Shortcut to getRequest
*
*/
protected function _getRequest()
{
return Mage::app()->getRequest();
}
}
使用我添加的自定义产品数据正确保存了产品 - 一旦保存了产品,该错误就不会在后续保存相同产品时发生。仅在首次创建产品时才会发生错误。
提前致谢