我正在使用Magento 1.7.0.2,但我严重卡在该问题中。
问题是,当我第一次搜索术语gem时,它什么都没有显示,您可以看到here这是我第一次搜索术语gem时

,当我再次搜索相同的字词时,Magento显示以下错误

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '1-512' for key 'PRIMARY'

Trace:
#0 /home/stagerig/public_html/includes/src/Varien_Db_Statement_Pdo_Mysql.php(110): Zend_Db_Statement_Pdo->_execute(Array)
#1 /home/stagerig/public_html/includes/src/__default.php(63012): Varien_Db_Statement_Pdo_Mysql->_execute(Array)
#2 /home/stagerig/public_html/includes/src/__default.php(52694): Zend_Db_Statement->execute(Array)
#3 /home/stagerig/public_html/includes/src/__default.php(53730): Zend_Db_Adapter_Abstract->query('INSERT INTO srl...', Array)
#4 /home/stagerig/public_html/includes/src/__default.php(54566): Zend_Db_Adapter_Pdo_Abstract->query('INSERT INTO srl...', Array)
#5 /home/stagerig/public_html/includes/src/MageCoders_DefaultSearch_Model_Mysql4_Fulltext.php(27): Varien_Db_Adapter_Pdo_Mysql->query('INSERT INTO srl...')
#6 /home/stagerig/public_html/includes/src/Mage_CatalogSearch_Model_Fulltext.php(136): MageCoders_DefaultSearch_Model_Mysql4_Fulltext->prepareResult(Object(Mage_CatalogSearch_Model_Fulltext), 'gem', Object(Mage_CatalogSearch_Model_Query))
#7 /home/stagerig/public_html/includes/src/Mage_CatalogSearch_Model_Resource_Fulltext_Collection.php(55): Mage_CatalogSearch_Model_Fulltext->prepareResult()
#8 /home/stagerig/public_html/includes/src/Mage_CatalogSearch_Model_Layer.php(58): Mage_CatalogSearch_Model_Resource_Fulltext_Collection->addSearchFilter('gem')
#9 /home/stagerig/public_html/includes/src/Mage_CatalogSearch_Model_Layer.php(42): Mage_CatalogSearch_Model_Layer->prepareProductCollection(Object(Mage_CatalogSearch_Model_Resource_Fulltext_Collection))


这些只是前几行,不是完整的,其他所有错误本质上都是相同的,表明文件存在一些问题...

我调查了这个问题,发现


magento数据库中的catalogsearch_result表具有名为PRIMARY的索引,并且由于该表而发生错误
catalogsearch_quer y表记录搜索词
1062 Duplicate entry '1-512' for key ' PRIMARY '中的1是搜索查询词ID和数据库,而512是与gem查询工作相关的产品ID
我认为当catalogusearch_result表再次尝试更新查询字符串时会发生此错误,因为记录已经存在,因此会弹出此错误...


完成了..
 5.我清空了数据库中的所有日志表


我清空了catalogsearch_results和catalogsearch_query表,但此错误再次弹出
我下载了正确的magento版本,甚至替换了完整的catalagsearch文件夹,但没有成功。
我尝试了re-indexing,完全清除了magento缓存(通过后端admin和手动删除var文件夹)
triead禁用CATALOGSEARCH模块并重新启用它...
试图从magento论坛和google搜索中获取帮助,但事实证明没有任何帮助。


我尝试了我遇到的每一个选择,或者我来到了knw,我在过去的四天里一直在head头,但都是徒劳的...

有人可以帮我吗,谢谢

对于阿兰

class MageCoders_DefaultSearch_Model_Mysql4_Fulltext extends Mage_CatalogSearch_Model_Mysql4_Fulltext{

    public function prepareResult($object, $queryText, $query)
    {
         $adapter = $this->_getWriteAdapter();

         if ($query->getIsProcessed()) { return $this; }
         $mainTable = $this->getTable('catalogsearch/result');
         $queryText = mysql_escape_string(trim($queryText));
         $product = Mage::getModel('catalog/product')->loadByAttribute('sku',$queryText);
         if($product){
            $sql = "INSERT INTO {$mainTable} SET query_id = '".$query->getId()."',
                                                 product_id = '".$product->getId()."',
                                                 relevance = '2'";
            $adapter->query($sql);
         }else{
            $name = '%'.$queryText.'%';
            $collection = Mage::getResourceModel('catalog/product_collection')
                                ->addAttributeToFilter('name',array('like'=>$name));

            if($collection->count()){
                foreach($collection as $item){
                        $sql = "INSERT INTO {$mainTable} SET query_id = '".$query->getId()."',
                                                             product_id = '".$item->getId()."',
                                                             relevance = '1'";
                        $adapter->query($sql);
                }
            }
         }

    }


这是您提到的文件中的代码,

对我来说,数据正确传递,存在逻辑错误或类似错误,对此不太确定

看看你能不能给我更多指导。

谢谢..

最佳答案

@newbee如果我建议只是卸载此扩展,您会成功吗?您的代码执行的操作与原始magento文件中执行的操作基本相同。唯一的办法不是很好。此代码将query_id,product_id及其相关性插入catalogsearch_results表中。问题在于,对于每个结果至少有一个产品的搜索查询,都会执行此查询。

对query_id-product_id应该是唯一的。但是扩展名试图在客户每次发送搜索查询时添加新行。

标准的magento全文本资源检查重复项,并使用“ ON DUPLICATE KEY UPDATE”指令进行插入。因此避免了查询重复。

关于php - 搜索项目时出现Magento错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12866292/

10-09 18:02
查看更多