如何在 joinField In Zend Framework Magento 中有一个 where like 查询



>我有一个查询,可以过滤产品模块的列名。

这是我的准备专栏

protected function _prepareColumns()
{
   $this->addColumn('name',
        array(
            'header'=> Mage::helper('catalog')->__('Name'),
            'index' => 'name',
    ));
   return parent::_prepareColumns();
}

现在这里是过滤功能

protected function _addColumnFilterToCollection($column)
{
    if ($this->getCollection()) {
        if ($column->getId() == 'websites') {
            $this->getCollection()
                 ->joinField('websites',
                'catalog/product_website',
                'website_id',
                'product_id=entity_id',
                null,
                'left');
        }
    }
    return parent::_addColumnFilterToCollection($column);
}

但如果项目名称为DOG SILVER CHAIN

如果我搜索DOG CHAIN

它不会返回DOG SILVER CHAIN

如何使过滤动态化,并且必须接受复杂的数据。

谢谢

使用filter_condition_callback。您可以在 MageAdminhtmlBlockCmsPageGrid 中看到一个示例。以下是您的使用方法:

$this->addColumn('name',
    array(
        'header'=> Mage::helper('catalog')->__('Name'),
        'index' => 'name',
        'filter_condition_callback' => array($this, '_filterNameCondition')
));

然后添加 filter 函数,该函数将值分解为单词并为每个单词添加一个条件:

protected function _filterNameCondition($collection, $column) {
    if(!$value = $column->getFilter()->getValue()) {
        return;
    }
    $field = ($column->getFilterIndex()) ? $column->getFilterIndex() : $column->getIndex();
    $collection->joinAttribute($field, 'catalog_product/name', 'entity_id', null, 'inner');
    foreach(explode(' ', $value) as $word) {
        $collection->addAttributeToFilter($field, array('like'=>'%'.$word.'%'));
    }
}

最新更新