我正在尝试覆盖Magento的目录搜索功能,以便使用“AND”而不是“OR”搜索搜索词。
执行此操作的“正确”方法,即更新证明,是创建我自己的模块。所以这就是我所做的。不幸的是,这不起作用。未调用更新的方法。
这是配置文件部分:
<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...");
}
}
我曾经读到某个地方,如果您实际上试图覆盖的类永远不会使用特定的类'creator'方法调用,那么这个'覆盖'是没有意义的吗?如果这是真的,这可以解释为什么我的新类方法从未使用过?
那么在这种情况下,如何覆盖此方法?我在这里做对了吗?
答案 0 :(得分:3)
我已成功覆盖相同的功能,看看代码: etc-&GT;模块: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-&gt; Modal-&gt;资源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;
}
}
答案 1 :(得分: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...');
}
}
答案 2 :(得分:0)
正如阿什利指出的那样,你必须尊重你所覆盖的方法的声明 有点像这样:
class MyNameSpace_MyModule_Model_CatalogSearch_Fulltext extends Mage_CatalogSearch_Model_Fulltext
{
public function prepareResult($query = null, $object = null, $queryText = null)
{
die('It works...');
}
}