avstudnitz / AvS_FastSimpleImport

Wrapper for Magento ImportExport functionality, which imports products and customers from arrays
308 stars 146 forks source link

Updating simple product causing exception #305

Open pawel-detka opened 8 years ago

pawel-detka commented 8 years ago

Indexer : update on save Partial Indexing: Yes

Updating qty/price existing products sometimes causing crash.

Method:

$product_list = $this->getListProductToUpdateQty(); //custom collection contains path to update xml
foreach ($product_list as $product) {
    $xml = simplexml_load_file($product['xml']);
    $data_to_add = array();
    $qty_real = $xml->ITEM->QTY - $xml->ITEM->QTYBOOKED;
    if ($qty_real <= 0) {
        $is_in_stock = 0;
    } else {
        $is_in_stock = 1;
    }
    $data_to_add[] = array(
        'sku' => (string)$xml->ITEM->SYMBOL,
        'qty' => (string)$qty_real,
        'is_in_stock' => $is_in_stock,

    );

    if (!empty($data_to_add)) {

        try {
            Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
            $import = Mage::getModel('fastsimpleimport/import');
            $import
                ->setUseNestedArrays(true)
                ->processProductImport($data_to_add);
            if (!empty ($import->getErrorMessages())) {
                $this->log('ERROR', 'updateProductQty', $_helper->__('Error 1 while updating products'));
                $errors = (array_keys($import->getErrorMessages()));
                foreach ($errors as $error) {
                    $this->log('ERROR', 'updateProductQty', $error);

                }
            } else {
                $this->log('INFO', 'updateProductQty', $_helper->__('Products successfully updated'));

            }
        } catch (Exception $e) {
            $this->log('ERROR', 'updateProductQty', $_helper->__('Error 2 while updating products: '));
            $errors = (array_keys($import->getErrorMessages()));
            Mage::logException($e);
            foreach ($errors as $error) {
                $this->log('ERROR', 'updateProductQty', $error);
            }
        }
    }
}

Log:

2016-03-14T14:02:19+00:00 ERR (3):
PDOException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '7906-0-1' for key 'PRIMARY' in /path/to/app/htdocs/lib/Zend/Db/Statement/Pdo.php:228
Stack trace:
#0 /path/to/app/htdocs/lib/Zend/Db/Statement/Pdo.php(228): PDOStatement->execute(Array)
#1 /path/to/app/htdocs/lib/Varien/Db/Statement/Pdo/Mysql.php(110): Zend_Db_Statement_Pdo->_execute(Array)
#2 /path/to/app/htdocs/app/code/core/Zend/Db/Statement.php(291): Varien_Db_Statement_Pdo_Mysql->_execute(Array)
#3 /path/to/app/htdocs/lib/Zend/Db/Adapter/Abstract.php(480): Zend_Db_Statement->execute(Array)
#4 /path/to/app/htdocs/lib/Zend/Db/Adapter/Pdo/Abstract.php(238): Zend_Db_Adapter_Abstract->query('INSERT INTO 'ca...', Array)
#5 /path/to/app/htdocs/lib/Varien/Db/Adapter/Pdo/Mysql.php(504): Zend_Db_Adapter_Pdo_Abstract->query('INSERT INTO 'ca...', Array)
#6 /path/to/app/htdocs/app/code/core/Mage/Catalog/Model/Resource/Product/Indexer/Price/Default.php(564): Varien_Db_Adapter_Pdo_Mysql->query('INSERT INTO 'ca...')
#7 /path/to/app/htdocs/app/code/core/Mage/Catalog/Model/Resource/Product/Indexer/Price/Configurable.php(71): Mage_Catalog_Model_Resource_Product_Indexer_Price_Default->_movePriceDataToIndexTable()
#8 /path/to/app/htdocs/app/code/core/Mage/Catalog/Model/Resource/Product/Indexer/Price.php(315): Mage_Catalog_Model_Resource_Product_Indexer_Price_Configurable->reindexEntity(Array)
#9 /path/to/app/htdocs/app/code/core/Mage/Catalog/Model/Resource/Product/Indexer/Price.php(246): Mage_Catalog_Model_Resource_Product_Indexer_Price->reindexProductIds(Array)
#10 /path/to/app/htdocs/app/code/community/AvS/FastSimpleImport/Model/Import/Entity/Product.php(638): Mage_Catalog_Model_Resource_Product_Indexer_Price->catalogProductMassAction(Object(Mage_Index_Model_Event))
#11 /path/to/app/htdocs/app/code/community/AvS/FastSimpleImport/Model/Import/Entity/Product.php(604): AvS_FastSimpleImport_Model_Import_Entity_Product->_reindexUpdatedProducts()
#12 /path/to/app/htdocs/app/code/community/AvS/FastSimpleImport/Model/Import.php(555): AvS_FastSimpleImport_Model_Import_Entity_Product->reindexImportedProducts()
#13 /path/to/app/htdocs/app/code/community/AvS/FastSimpleImport/Model/Import.php(118): AvS_FastSimpleImport_Model_Import->reindexImportedProducts()
#14 /path/to/app/htdocs/app/code/local/Custom/Module/Model/Product.php(534): AvS_FastSimpleImport_Model_Import->processProductImport(Array)
#15 /path/to/app/htdocs/app/code/local/Custom/Module/Model/Cron.php(135): Custom_Module_Model_Product->updateProduct()
#16 /path/to/app/htdocs/app/code/core/Mage/Cron/Model/Observer.php(326): Custom_Module_Model_Cron->processAll(Object(Mage_Cron_Model_Schedule))
#17 /path/to/app/htdocs/app/code/core/Mage/Cron/Model/Observer.php(72): Mage_Cron_Model_Observer->_processJob(Object(Mage_Cron_Model_Schedule), Object(Mage_Core_Model_Config_Element))
#18 /path/to/app/htdocs/app/code/core/Mage/Core/Model/App.php(1358): Mage_Cron_Model_Observer->dispatch(Object(Varien_Event_Observer))
#19 /path/to/app/htdocs/app/code/core/Mage/Core/Model/App.php(1337): Mage_Core_Model_App->_callObserverMethod(Object(Mage_Cron_Model_Observer), 'dispatch', Object(Varien_Event_Observer))
#20 /path/to/app/htdocs/app/Mage.php(448): Mage_Core_Model_App->dispatchEvent('default', Array)
#21 /path/to/app/htdocs/cron.php(78): Mage::dispatchEvent('default')
#22 {main}

Next Zend_Db_Statement_Exception: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '7906-0-1' for key 'PRIMARY', query was: INSERT INTO `catalog_product_index_price_tmp` SELECT `catalog_product_index_price_final_tmp`.`entity_id`, `catalog_product_index_price_final_tmp`.`customer_group_id`, `catalog_product_index_price_final_tmp`.`website_id`, `catalog_product_index_price_final_tmp`.`tax_class_id`, `catalog_product_index_price_final_tmp`.`orig_price` AS `price`, `catalog_product_index_price_final_tmp`.`price` AS `final_price`, `catalog_product_index_price_final_tmp`.`min_price`, `catalog_product_index_price_final_tmp`.`max_price`, `catalog_product_index_price_final_tmp`.`tier_price`, `catalog_product_index_price_final_tmp`.`group_price` FROM `catalog_product_index_price_final_tmp` in /path/to/app/htdocs/lib/Zend/Db/Statement/Pdo.php:235
Stack trace:
#0 /path/to/app/htdocs/lib/Varien/Db/Statement/Pdo/Mysql.php(110): Zend_Db_Statement_Pdo->_execute(Array)
#1 /path/to/app/htdocs/app/code/core/Zend/Db/Statement.php(291): Varien_Db_Statement_Pdo_Mysql->_execute(Array)
#2 /path/to/app/htdocs/lib/Zend/Db/Adapter/Abstract.php(480): Zend_Db_Statement->execute(Array)
#3 /path/to/app/htdocs/lib/Zend/Db/Adapter/Pdo/Abstract.php(238): Zend_Db_Adapter_Abstract->query('INSERT INTO `ca...', Array)
#4 /path/to/app/htdocs/lib/Varien/Db/Adapter/Pdo/Mysql.php(504): Zend_Db_Adapter_Pdo_Abstract->query('INSERT INTO `ca...', Array)
#5 /path/to/app/htdocs/app/code/core/Mage/Catalog/Model/Resource/Product/Indexer/Price/Default.php(564): Varien_Db_Adapter_Pdo_Mysql->query('INSERT INTO `ca...')
#6 /path/to/app/htdocs/app/code/core/Mage/Catalog/Model/Resource/Product/Indexer/Price/Configurable.php(71): Mage_Catalog_Model_Resource_Product_Indexer_Price_Default->_movePriceDataToIndexTable()
#7 /path/to/app/htdocs/app/code/core/Mage/Catalog/Model/Resource/Product/Indexer/Price.php(315): Mage_Catalog_Model_Resource_Product_Indexer_Price_Configurable->reindexEntity(Array)
#8 /path/to/app/htdocs/app/code/core/Mage/Catalog/Model/Resource/Product/Indexer/Price.php(246): Mage_Catalog_Model_Resource_Product_Indexer_Price->reindexProductIds(Array)
#9 /path/to/app/htdocs/app/code/community/AvS/FastSimpleImport/Model/Import/Entity/Product.php(638): Mage_Catalog_Model_Resource_Product_Indexer_Price->catalogProductMassAction(Object(Mage_Index_Model_Event))
#10 /path/to/app/htdocs/app/code/community/AvS/FastSimpleImport/Model/Import/Entity/Product.php(604): AvS_FastSimpleImport_Model_Import_Entity_Product->_reindexUpdatedProducts()
#11 /path/to/app/htdocs/app/code/community/AvS/FastSimpleImport/Model/Import.php(555): AvS_FastSimpleImport_Model_Import_Entity_Product->reindexImportedProducts()
#12 /path/to/app/htdocs/app/code/community/AvS/FastSimpleImport/Model/Import.php(118): AvS_FastSimpleImport_Model_Import->reindexImportedProducts()
#13 /path/to/app/htdocs/app/code/local/Custom/Module/Model/Product.php(534): AvS_FastSimpleImport_Model_Import->processProductImport(Array)
#14 /path/to/app/htdocs/app/code/local/Custom/Module/Model/Cron.php(135): Custom_Module_Model_Product->updateProduct()
#15 /path/to/app/htdocs/app/code/core/Mage/Cron/Model/Observer.php(326): Custom_Module_Model_Cron->processAll(Object(Mage_Cron_Model_Schedule))
#16 /path/to/app/htdocs/app/code/core/Mage/Cron/Model/Observer.php(72): Mage_Cron_Model_Observer->_processJob(Object(Mage_Cron_Model_Schedule), Object(Mage_Core_Model_Config_Element))
#17 /path/to/app/htdocs/app/code/core/Mage/Core/Model/App.php(1358): Mage_Cron_Model_Observer->dispatch(Object(Varien_Event_Observer))
#18 /path/to/app/htdocs/app/code/core/Mage/Core/Model/App.php(1337): Mage_Core_Model_App->_callObserverMethod(Object(Mage_Cron_Model_Observer), 'dispatch', Object(Varien_Event_Observer))
#19 /path/to/app/htdocs/app/Mage.php(448): Mage_Core_Model_App->dispatchEvent('default', Array)
#20 /path/to/app/htdocs/cron.php(78): Mage::dispatchEvent('default')
#21 {main}
avstudnitz commented 8 years ago

Well, it's the default product price indexer we are using here. I don't think we can do anything about it, I'd assume your database is slightly corrupt somehow.

pawel-detka commented 8 years ago

Reindex by shell/indexer.php and n98-magerun:index:reindex:all do not causing any problems.

Magento 1.9.2.4 @ nginx - php5.6 & php7.0 (two different environment)