Magento 自定义模块中的完整性约束违规 [英] Integrity constraint violation in Magento custom module

查看:22
本文介绍了Magento 自定义模块中的完整性约束违规的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个与 在 Magento 中创建产品的完整性约束违规 类似的问题(未回答),但我正在创建一个钩子的自定义观察者进入 catalog_product_save_after 事件 - 基于本教程:http://fishpig.co.uk/blog/custom-tabs-magento-product-admin.html

I have a similar probelm to Integrity constraint violation creating Product in Magento (unanswered) but I am creating a custom Observer that hooks into the catalog_product_save_after event - based on this tutorial: http://fishpig.co.uk/blog/custom-tabs-magento-product-admin.html

但是,每当保存新产品时,我都会收到此错误:

However whenever a new product is saved I get this error:

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

config.xml 如下所示:

The config.xml looks like this:

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

该产品与我添加的自定义产品数据一起正确保存 - 一旦产品被保存,同一产品的后续保存不会发生错误.只是在第一次创建产品时发生错误.

The product is saved correctly with the custom product data I'm adding - and once the product is saved the error does not occur on subsequent saves of the same product. It is just when the product is first created the error occurs.

提前致谢

推荐答案

不要使用 $product->save() 尝试使用资源模型,a la $product->getResource()->save($product).

Instead of using $product->save() try using the resource model, a la $product->getResource()->save($product).

原因是 $product->save() 将重新触发所有保存事件,因此运行任何保存目录库存的内容并抛出错误.

The reason being $product->save() will re-trigger all save events, hence running whatever is saving the cataloginventory_stock and throwing the error.

这篇关于Magento 自定义模块中的完整性约束违规的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆