EcomDev_UrlRewrite icon indicating copy to clipboard operation
EcomDev_UrlRewrite copied to clipboard

impossible to assign products to category when error_reporting(0);

Open OSdave opened this issue 11 years ago • 1 comments

I've discovered a bug in this module: when error_reporting is set to false, aka error_reporting(0);in the index.php file and Catalog URL Rewrites index set as "Update on Save", it is impossible to assign or unassign products to category.

Steps to reproduce:

  • set error reporting as false in index.php
  • set Catalog URL Rewrites index set as "Update on Save"
  • go to catalog > manage categories
  • assign or unassign a product to a category
  • click on "Save Category"

Actual result:

the "Please Wait" loading div never disappear. Firebug console indicates:

NetworkError: 500 Internal Server Error - http://magento1702.local/index.php/admin/catalog_category/save/key/2a7a1d7f31377d859e42b573383d55e1/id/22/?isAjax=true

Expected result:

the "Please Wait" loading div should disappear and the page should reload (native Magento behavior)

Why does this happen?

So far, this is what I have found out: the error is triggered in /lib/Varien/Db/Adapter/Pdo/Mysql.php, line 399:

protected function _checkDdlTransaction($sql)
{
    if (is_string($sql) && $this->getTransactionLevel() > 0) {
        $startSql = strtolower(substr(ltrim($sql), 0, 3));
        if (in_array($startSql, $this->_ddlRoutines)) {
            trigger_error(Varien_Db_Adapter_Interface::ERROR_DDL_MESSAGE, E_USER_ERROR);//line 399
        }
    }
}

the code gets there because it performs a truncate on ecomdev_urlrewrite_root_category and $this->getTransactionLevel() returns 2 at this point. This value of 2 is because Varien_Db_Adapter_Pdo_Mysql::beginTransaction is executed twice (first by Mage_Catalog_Model_Category::saveAction and then by Mage_Catalog_Model_Category::_afterSave) and Varien_Db_Adapter_Pdo_Mysql::commit (which would decrease the _transactionLevel value) is not executed

OSdave avatar May 21 '14 10:05 OSdave

here's a proposal to solve this bug: EcomDev_UrlRewrite_Model_Mysql4_Indexer:

    public function catalogProductSave(Mage_Index_Model_Event $event)
    {
        $eventData = $event->getNewData();
        $productIds = isset($eventData['rewrite_product_ids']) ? $eventData['rewrite_product_ids'] : null;
        $categoryIds = isset($eventData['rewrite_category_ids']) ? $eventData['rewrite_category_ids'] : null;

        Mage::register('rewrite_product_ids', $productIds);
        Mage::register('rewrite_category_ids', $categoryIds);

        return $this;
    }

    /**
     * Updates category rewrites on after save event operation
     *
     * @param Mage_Index_Model_Event $event
     */
    public function catalogCategorySave(Mage_Index_Model_Event $event)
    {
        $eventData = $event->getNewData();
        Mage::register('rewrite_category_ids', $eventData['rewrite_category_ids']);

        return $this;
    }

config.xml

    <adminhtml>
        <events>
            <catalog_category_save_commit_after>
                <observers>
                    <reindex_category_catalog_url_rewrite>
                        <class>ecomdev_urlrewrite/observer</class>
                        <method>reindexCategoryUrlRewrite</method>
                    </reindex_category_catalog_url_rewrite>
                </observers>
            </catalog_category_save_commit_after>
            <catalog_product_save_commit_after>
                <observers>
                    <reindex_product_catalog_url_rewrite>
                        <class>ecomdev_urlrewrite/observer</class>
                        <method>reindexProductUrlRewrite</method>
                    </reindex_product_catalog_url_rewrite>
                </observers>
            </catalog_product_save_commit_after>
        </events>
    </adminhtml>

EcomDev_UrlRewrite_Model_Observer:

class EcomDev_UrlRewrite_Model_Observer
{

    public function reindexCategoryUrlRewrite()
    {
        $rewriteIds = Mage::registry('rewrite_category_ids');

        Mage::getResourceModel('ecomdev_urlrewrite/indexer')->updateCategoryRewrites($rewriteIds);
        return $this;
    }

    public function reindexProductUrlRewrite()
    {
        $productIds = Mage::registry('rewrite_product_ids');
        $categoryIds = Mage::registry('rewrite_category_ids');

        Mage::getResourceModel('ecomdev_urlrewrite/indexer')->updateProductRewrites($productIds, $categoryIds);
        return $this;
    }

}

what do you think?

OSdave avatar May 27 '14 11:05 OSdave