2013-09-28 29 views
2

我試圖重寫Magento的目錄搜索功能,以便搜索條件使用'AND'而不是'OR'進行搜索。Magento類方法忽略不工作

做到這一點的「正確」方式,即更新證明,是創建我自己的模塊。所以這就是我所做的。不幸的是,這是行不通的。更新的方法未被調用。

這裏的配置文件部分:

<global> 
     <models> 
      <catalogsearch> 
       <rewrite> 
        <fulltext>MyNameSpace_MyModule_Model_CatalogSearch_Fulltext</fulltext> 
       </rewrite> 
      </catalogsearch> 
     </models> 
</global> 

我想覆蓋的方法是Mage_CatalogSearch_Model_Fulltext,和我其實在做一個「擴展」 - 例如,

class MyNameSpace_MyModule_Model_CatalogSearch_Fulltext extends Mage_CatalogSearch_Model_Fulltext 
{ 
    public function prepareResult($object, $queryText, $query) 
    { 
     die("It works..."); 
    } 
} 

我如果你真的試圖重寫的類從來沒有用特定類的創建者方法調用,那麼在某處讀取「重寫」是沒有意義的。如果這是真的,這可能解釋爲什麼我的新類方法從來沒有使用過?

那麼在那種情況下,如何覆蓋這個方法呢?我在這裏做正確的事情嗎?

+2

你的定義已經很好了。我注意到你的覆蓋函數有更多的參數,所以你應該給這些默認值(默認magento只有$ query = null)。您是否清除了緩存並檢查了啓用模塊中顯示的模塊。系統或異常日誌中的任何錯誤? – Ashley

+0

謝謝阿什利 - 好的第一 - 函數聲明只是一個剪切和粘貼工作 - 不應該有任何額外的參數?!另外 - 系統或異常日誌中沒有任何內容。 – Mat

+0

對不起 - 也已清除緩存和模塊顯示爲啓用 – Mat

回答

0

正如Ashley指出的那樣,您必須尊重您壓倒一切的方法的聲明。
成才這樣工作的:

class MyNameSpace_MyModule_Model_CatalogSearch_Fulltext extends Mage_CatalogSearch_Model_Fulltext 
{ 

    public function prepareResult($query = null, $object = null, $queryText = null) 
    { 
     die('It works...'); 
    } 
} 
1

你config.xml的下方喜歡。

<global> 
     <models> 
      <catalogsearch> 
       <rewrite> 
        <catalogsearch_fulltext>MyNameSpace_MyModule_Model_CatalogSearch_Fulltext</catalogsearch_fulltext> 
       </rewrite> 
      </catalogsearch> 
     </models> 
</global> 

而你的模型類應如下所示。

class MyNameSpace_MyModule_Model_CatalogSearch_Fulltext extends Mage_CatalogSearch_Model_Fulltext 
    { 
    // you should put default declaration of the function here. 
// inside the function change the development as you want. 
     public function prepareResult($query = null) 
     { 
      die('It works...'); 
     } 
    } 
+0

嗨,謝謝。對不起,這完全沒有區別。任何其他想法? – Mat

+0

也有什麼原因,你擴展Mage_CatalogSearch_Model_Fulltext而不是Mage_CatalogSearch_Model_Resource_Fulltext?或者它沒有關係? – Mat

+0

另外 - 我沒有遵循你的配置文件的邏輯?在標記之後不應該爲標記,那麼在標記之後標記爲 ??當然,在原始的catalogsearch文件夾下沒有名爲catalogsearch的文件夾? – Mat

3

我已經成功覆蓋相同的功能看代碼: etc->模塊:LevoSoft_CatalogSearch.xml

<?xml version="1.0"?> 
 
<config> 
 
    <modules> 
 
     <LevoSoft_CatalogSearch> 
 
      <active>true</active> 
 
      <codePool>local</codePool> 
 
     </LevoSoft_CatalogSearch> 
 
    </modules> 
 
</config>

config.xml中

<config> 
 
    <modules> 
 
     <LevoSoft_CatalogSearch> 
 
      <version>1.0.0</version> 
 
     </LevoSoft_CatalogSearch> 
 
    </modules> 
 
    <global> 
 
     <models> 
 
      <catalogsearch_resource> 
 
       <rewrite> 
 
        <fulltext>LevoSoft_CatalogSearch_Model_Resource_Fulltext</fulltext> 
 
       </rewrite> 
 
      </catalogsearch_resource> 
 
     </models> 
 
    </global> 
 

 
</config>

CatalogSearch-> Modal->資源Fullteext.php

class LevoSoft_CatalogSearch_Model_Resource_Fulltext extends Mage_CatalogSearch_Model_Resource_Fulltext 
 
{ 
 
    /** 
 
    * Prepare results for query with AND operator which will give results as per client's need 
 
    * 
 
    * @param Mage_CatalogSearch_Model_Fulltext $object 
 
    * @param string $queryText 
 
    * @param Mage_CatalogSearch_Model_Query $query 
 
    * @return Mage_CatalogSearch_Model_Resource_Fulltext 
 
    */ 
 
    public function prepareResult($object, $queryText, $query) 
 
    { 
 
     //var_dump("hasaannnnnnnn");exit; 
 
     $adapter = $this->_getWriteAdapter(); 
 
     if (!$query->getIsProcessed()) { 
 
      $searchType = $object->getSearchType($query->getStoreId()); 
 

 
      $preparedTerms = Mage::getResourceHelper('catalogsearch') 
 
       ->prepareTerms($queryText, $query->getMaxQueryWords()); 
 

 
      $bind = array(); 
 
      $like = array(); 
 
      $likeCond = ''; 
 
      if ($searchType == Mage_CatalogSearch_Model_Fulltext::SEARCH_TYPE_LIKE 
 
       || $searchType == Mage_CatalogSearch_Model_Fulltext::SEARCH_TYPE_COMBINE 
 
      ) { 
 
       $helper = Mage::getResourceHelper('core'); 
 
       $words = Mage::helper('core/string')->splitWords($queryText, true, $query->getMaxQueryWords()); 
 
       foreach ($words as $word) { 
 
        $like[] = $helper->getCILike('s.data_index', $word, array('position' => 'any')); 
 
       } 
 
       if ($like) { 
 
        $likeCond = '(' . join(' AND ', $like) . ')'; 
 
       } 
 
      } 
 
      $mainTableAlias = 's'; 
 
      $fields = array(
 
       'query_id' => new Zend_Db_Expr($query->getId()), 
 
       'product_id', 
 
      ); 
 
      $select = $adapter->select() 
 
       ->from(array($mainTableAlias => $this->getMainTable()), $fields) 
 
       ->joinInner(array('e' => $this->getTable('catalog/product')), 
 
        'e.entity_id = s.product_id', 
 
        array()) 
 
       ->where($mainTableAlias.'.store_id = ?', (int)$query->getStoreId()); 
 

 
      if ($searchType == Mage_CatalogSearch_Model_Fulltext::SEARCH_TYPE_FULLTEXT 
 
       || $searchType == Mage_CatalogSearch_Model_Fulltext::SEARCH_TYPE_COMBINE 
 
      ) { 
 
       $bind[':query'] = implode(' ', $preparedTerms[0]); 
 
       $where = Mage::getResourceHelper('catalogsearch') 
 
        ->chooseFulltext($this->getMainTable(), $mainTableAlias, $select); 
 
      } 
 

 
      if ($likeCond != '' && $searchType == Mage_CatalogSearch_Model_Fulltext::SEARCH_TYPE_COMBINE) { 
 
        $where .= ($where ? ' OR ' : '') . $likeCond; 
 
      } elseif ($likeCond != '' && $searchType == Mage_CatalogSearch_Model_Fulltext::SEARCH_TYPE_LIKE) { 
 
       $select->columns(array('relevance' => new Zend_Db_Expr(0))); 
 
       $where = $likeCond; 
 
      } 
 

 
      if ($where != '') { 
 
       $select->where($where); 
 
      } 
 

 
      $sql = $adapter->insertFromSelect($select, 
 
       $this->getTable('catalogsearch/result'), 
 
       array(), 
 
       Varien_Db_Adapter_Interface::INSERT_ON_DUPLICATE); 
 
      $adapter->query($sql, $bind); 
 

 
      $query->setIsProcessed(1); 
 
     } 
 

 
     return $this; 
 
    } 
 
}