Magento类方法覆盖不起作用


Magento Class Method Override Not Working

我正在尝试覆盖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"方法调用,那么进行这种"覆盖"是没有意义的?如果这是真的,这可以解释为什么我的新类方法从未被使用过?

那么在这种情况下,如何覆盖此方法?我在这里做正确的事情吗?

我已成功覆盖相同的功能,查看代码:等->模块:LevoSoft_CatalogSearch.xml

<?xml version="1.0"?>
<config>
    <modules>
        <LevoSoft_CatalogSearch>
            <active>true</active>
            <codePool>local</codePool>
        </LevoSoft_CatalogSearch>
    </modules>
</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->Resource 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;
    }
}

您的配置.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...');
        }
    }

正如 Ashley 所指出的,你必须尊重你正在覆盖的方法的声明。
像这样工作的一些:

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