Question

I would like to truncate magento core_url_rewrite table and reindex catalog url rewrites via an upgrade script. The table is truncated but the reindexing catalog url is not working. any help please..

//truncate magento core_url_rewrite table
$installer = $this;
$installer->startSetup();
$installer->run(
"TRUNCATE TABLE `{$this->getTable('core_url_rewrite')}` "
);

$installer->endSetup();

// reindex catalog url rewrites
$indexCatUrl = Mage::getSingleton('index/indexer')->getProcessByCode('catalog_url');
try {

$indexCatUrl->reindexAll();

} catch(Exception $e) {
$this->log('Error Reindex all ' . $e);
throw ($e);
}
Was it helpful?

Solution

The short version is that the reindex should be performed using a data setup script rather than a sql script. To understand why, we need to understand a little about the Magento's bootstrap process...

Magento has two types of setup scripts. There are sql scripts and data scripts. The sql scripts live in the "sql/nameofsetupresource_setup/" folder and are executed relatively early in Magento's bootstrap process. SQL scripts are intended to be used for schema changes and the like, and not for data manipulation. When Magento executes the sql scripts, it does so in a special "update mode", and various API calls (especially around Multi-store functionality) don't work (or don't work correctly in this mode).

Once all of the sql scripts, and some more of the Magento bootstrap process occurs, then a second batch of setup scripts are run. "data" scripts live in "data/nameofsetupresource_setup" and are named "data-upgrade-0.1.0-0.1.1.php" instead of "mysql4-upgrade-0.1.0-0.1.1.php". These "data" scripts are intended to be used to insert/manipulate data in our database once we've got the schema up to date using sql scripts. Data scripts are not executed in update mode, and the Magento core is fully initialised and ready for use.

All of the Magento indexers are concerned with building the correct data for each "store" in your install. Even if you're not using the multi-store functionality in Magento, the indexer needs to look up the available store codes, etc to do it's job correctly. When trying to reindex in a sql script the store data isn't available, but when run in a data script, the indexer can get the information it needs and works correctly.

For more information regarding data script, this blog post from Inchoo should steer you in the right direction.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top