Magento:以编程方式重建平面目录
我使用cron每晚导入库存更改。当我试图更改产品信息(价格等)时,会出现以下错误:Magento:以编程方式重建平面目录,magento,Magento,我使用cron每晚导入库存更改。当我试图更改产品信息(价格等)时,会出现以下错误: Column not found: 1054 Unknown column 'e.display_price_group_0' in 'field list' 我可以通过单击缓存管理面板中的“重建平面目录产品”来修复此问题。我使用以下代码设置了一个cron以编程方式执行此操作: Mage :: getResourceModel( 'catalog/product_flat_indexer' ) -> re
Column not found: 1054 Unknown column 'e.display_price_group_0' in 'field list'
我可以通过单击缓存管理面板中的“重建平面目录产品”来修复此问题。我使用以下代码设置了一个cron以编程方式执行此操作:
Mage :: getResourceModel( 'catalog/product_flat_indexer' ) -> rebuild();
我在运行脚本时没有收到任何错误,但“未找到列”错误仍然存在
除了通过管理界面,有人知道我如何重建平面目录吗?请参见此。
我个人对此有一些问题,但其他人似乎对此很满意。如果您不想要整个产品,您可以轻松地取出重建平面目录产品的部件,并将cron作业指向它。我也无法让它正常工作 当我从管理员处重建rebuild Flat Catalog产品时,它工作正常,并且我没有收到SQL列错误,但当我以编程方式执行时,它无法通过以下方式工作:
Mage::getResourceModel('catalog/product_flat_indexer')->rebuild() 之前我说过要这样做:
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();
基本上,只要重建一切。不要担心优化。就像有人曾经说过的,“过早优化是万恶之源。”我发现有一种更有效的方法可以只更新特定的产品属性
Mage::getModel('catalog/product_flat_indexer')->updateAttribute($attributeCode, null, $productIds);
也可以在平面表格中更新整个产品:
Mage::getModel('catalog/product_flat_indexer')->updateProduct($productIds, null);
其中$productIds是要更新的产品实体ID的数组。这些函数还将更新与您更新的产品相关的其他索引数据。希望这有帮助。我刚刚基于Shell reindex脚本编写了这段代码。 我已经在Magento 1.5.1中使用web脚本测试了它(最大执行时间长)
人们为什么不在发布一些不起作用的东西之前先研究一下,打开shell/indexer.php,在里面你会找到所有与索引相关的答案。这是我从中获得上述代码的脚本。Laizer,你遇到的麻烦是,你收到了错误“exception‘Zend_Db_Statement_exception’,消息是‘SQLSTATE[42S22]:Column not found:1054未知列‘e.display_price_group_0’,在/var/www/releases/20101019/lib/Zend/Db/Statement/Pdo.php:238的‘字段列表’中”在重建平面目录后更新产品时?如果是,您是如何修复的?谢谢恐怕我帮不了你。我想我从来没有见过这个问题。我很高兴知道我不是唯一一个,我以为我疯了。
Mage::getModel('catalog/product_flat_indexer')->updateProduct($productIds, null);
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();
}
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);
}
}
}