Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
470 views
in Technique[技术] by (71.8m points)

sql - Integrity constraint violation in Magento custom module

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'

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>

The outline of the class is like this:

<?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.

Thanks in advance

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...