在Magento中实现搜索查询

时间:2013-07-05 10:35:20

标签: php mysql magento

SELECT * FROM `table` WHERE
(`name` LIKE '$search' 
OR `address` LIKE '$search'
OR `name` LIKE '$search%'
OR `address` LIKE '$search%'
OR `name` LIKE '%$search%'
OR `address` LIKE '%$search%'
OR `name` like '%$search'
OR `address` like '%$search')
order by 
( (CASE WHEN `name` = '$search' THEN 1 ELSE 0 END)
+ (CASE WHEN `address` = '$search' THEN 1 ELSE 0 END)
+ (CASE WHEN `name` LIKE '$search%' THEN 1 ELSE 0 END)
+ (CASE WHEN `address` LIKE '$search%' THEN 1 ELSE 0 END)
+ (CASE WHEN `name` LIKE '%$search%' THEN 1 ELSE 0 END)
+ (CASE WHEN `address` LIKE '%$search%' THEN 1 ELSE 0 END)
+ (CASE WHEN `name` LIKE '%$search' THEN 1 ELSE 0 END)
+ (CASE WHEN `address` LIKE '%$search' THEN 1 ELSE 0 END))

我想在Magento中为搜索自定义模块执行这些操作并尝试放置$query->read("....."),但这会导致setCollection()中的分页问题。 到目前为止,我使用以下代码:

public function __construct()
{
    parent::__construct();
$search = Mage::app()->getRequest()->getParam("search");
    $collection = Mage::getModel('search/search')->getCollection()
            ->addFieldToFilter('name',array("like"=>"%$search%"));
    $this->setCollection($collection);
}

// next is for pagination
protected function _prepareLayout()
{
    parent::_prepareLayout();
    $pager = $this->getLayout()->createBlock('page/html_pager', 'custom.pager');
    $pager->setAvailableLimit(array(20=>20,25=>25,30=>30));
    $pager->**setCollection**($this->getCollection()); // this create probleme whit read() 
    $this->setChild('pager', $pager);
    $this->getCollection()->load();
    return $this;
}

public function getPagerHtml()
{
    return $this->getChildHtml('pager');
}

我想要是否可以使用Magento功能。

0 个答案:

没有答案