开发者

Magento: Rebuilding Flat Catalog Programmatically

I am using a cron to import inventory changes nightly. When I try to change a product's information (price, etc) I get the following error:

Column not found: 1054 Unknown column 'e.display_price_group_0' in 'field list'

I can fix this by clicking "Rebuild Flat Catalog Product" in the Cache Management panel. I setup a cron to do this programmatically using the following code:

Mage :: getResourceModel( 'catalog/product_flat_indexer' ) -> rebuild();

I don't get any errors when I run the script, but the "Column not found" error persists.

Doe开发者_开发问答s anyone know how I can rebuild the flat catalog other than through the admin interface?


Previous I said to do this:

Mage::getModel('catalog/product_flat_indexer')->rebuild();
Note: it's getModel and NOT getResourceModel.

This is not true. Either works. However, I have found through a rather painful trial and error process that the flat product tables don't rebuild correctly unless I also rebuild the entire catalog. This is how I finally solved my problem:

Mage::getSingleton('catalog/index')->rebuild();
Mage::getResourceModel('catalog/product_flat_indexer')->rebuild();
Mage::getSingleton('catalog/url')->refreshRewrites();
Mage::getModel('catalog/product_image')->clearCache();
Mage::getSingleton('catalogsearch/fulltext')->rebuildIndex();
Mage::getSingleton('cataloginventory/stock_status')->rebuild();
$flag = Mage::getModel('catalogindex/catalog_index_flag')->loadSelf();
if ($flag->getState() == Mage_CatalogIndex_Model_Catalog_Index_Flag::STATE_RUNNING) {
    $kill = Mage::getModel('catalogindex/catalog_index_kill_flag')->loadSelf();
    $kill->setFlagData($flag->getFlagData())->save();
}
$flag->setState(Mage_CatalogIndex_Model_Catalog_Index_Flag::STATE_QUEUED)->save();
Mage::getSingleton('catalogindex/indexer')->plainReindex();

Basically, just rebuild everything. Don't worry about optimization. Like someone once said, "Premature optimization is the root of all evil."


I've found that there's a more efficient way to update only specific product attributes.

Mage::getModel('catalog/product_flat_indexer')->updateAttribute($attributeCode, null, $productIds);

Or you can update the entire product in the flat table:

Mage::getModel('catalog/product_flat_indexer')->updateProduct($productIds, null);

Where $productIds is an array of the product entity ids to update. These functions will also update other indexed data related to the products you update. Hope this helps.


See this script. I personally had some trouble with it, but others seem to be quite happy with it.
If you don't want the whole thing, you can easily pull out the part that rebuilds the flat catalog product and point a cron job at it.


* Rebuild Catalog Index

  Mage::getSingleton('catalog/index')->rebuild();

* Rebuild Flat Catalog Product

  Mage::getResourceModel('catalog/product_flat_indexer')->rebuild();

* Inventory Stock

  Mage::getSingleton('cataloginventory/stock_status')->rebuild();


public function rebuildIndexes(){
    $processes = array();
    $collection = Mage::getSingleton('index/indexer')->getProcessesCollection();
    foreach ($collection as $process) {
        try {
            $process->reindexEverything();
            $this->_message($process->getIndexer()->getName() . " index was rebuilt successfully");
        } catch (Mage_Core_Exception $e) {
            $this->_throwException($e->getMessage());
        } catch (Exception $e) {
            $this->_throwException($process->getIndexer()->getName() . " index process unknown error:\n" . $e);
        }
    }
}

people why dont you research a little bit before post somethig that doesn't work, open the shell/indexer.php, inside you will find all the answers related to indexing.


I also can't get it to work correctly.

When I rebuild the Rebuild Flat Catalog Product from the Admin it works fine and I don't get the SQL column error, but when I do it programmatically it doesn't work via:

Mage::getResourceModel('catalog/product_flat_indexer')->rebuild();


I have just written this code, based on the Shell reindex script. I have tested it in Magento 1.5.1 using a web script (with long max_execution_time).

if ( !empty($_SERVER['HTTP_HOST']) )
{
    header('Content-Type: text/plain');
}    

$oIndexer = Mage::getSingleton('index/indexer');            
/* @var $oIndexer Mage_Index_Model_Indexer */
$oProcessCollection = $oIndexer->getProcessesCollection();  
/* @var $oProcessCollection Mage_Index_Model_Mysql4_Process_Collection */

foreach ( $oProcessCollection as $oProcess )
{
    /* @var $oProcess Mage_Index_Model_Process */
    echo 'Rebuilding ' . $oProcess->getIndexer()->getName() . ' index...';
    outputFlush();
    $oProcess->reindexEverything();
}

echo 'Done.';
outputFlush()

function outputFlush()
{
    while ( ob_get_length() )
    {
        ob_end_flush();
    }
    if ( !empty($_SERVER['HTTP_HOST']) )
    {
        echo str_repeat(' ',4096);
    }
    echo "\n";
    flush();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜