Magento:以编程方式重建平面目录

时间:2010-03-18 17:17:53

标签: magento

我使用cron每晚导入库存变化。当我尝试更改产品的信息(价格等)时,我收到以下错误:

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

我可以通过单击“缓存管理”面板中的“重建平面目录产品”来解决此问题。我使用以下代码设置了一个cron以编程方式执行此操作:

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

运行脚本时没有出现任何错误,但“找不到列”错误仍然存​​在。

除了通过管理界面,有没有人知道如何重建平面目录?

7 个答案:

答案 0 :(得分:4)

以前我说要这样做:

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

事实并非如此。要么有效。但是,我通过一个相当痛苦的试错过程发现平面产品表不能正确重建,除非我还重建整个目录。这就是我最终解决问题的方法:

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();

基本上,只需重建一切。不要担心优化。就像曾经说过的那样,“过早优化是所有邪恶的根源。”

答案 1 :(得分:3)

我发现有一种更有效的方法来更新特定的产品属性。<​​/ p>

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

或者您可以在平台中更新整个产品:

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

其中$ productIds是要更新的产品实体ID的数组。这些功能还将更新与您更新的产品相关的其他索引数据。希望这会有所帮助。

答案 2 :(得分:1)

script。 我个人遇到了一些麻烦,但其他人似乎对它很满意 如果你不想要整个东西,你可以轻松地拉出重建平面目录产品的部分并指出一个cron工作。

答案 3 :(得分:1)

* 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();

答案 4 :(得分:1)

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);
        }
    }
}

人们为什么不在发布不起作用的东西之前研究一下,打开shell / indexer.php,在里面你会发现所有与索引相关的答案。

答案 5 :(得分:0)

我也无法让它正常工作。

当我从管理员重建Rebuild Flat Catalog产品时,它工作正常,我没有得到SQL列错误,但是当我以编程方式执行它时,它无法通过以下方式工作:

法师:: getResourceModel( '目录/ product_flat_indexer') - &GT;重建();

答案 6 :(得分:0)

我刚刚根据Shell reindex脚本编写了这段代码。 我已经使用Web脚本(使用long max_execution_time)在Magento 1.5.1中对其进行了测试。

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();
}