简体   繁体   中英

How to have a where like query in joinField In Zend Framework Magento

I have a query which does the filtering of the column name of Products Module.

Here's my prepare column

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

    ));

   return parent::_prepareColumns();
}

Now here's the Filtering Function

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);
}

But If the Item Name is DOG SILVER CHAIN

If I search DOG CHAIN

it will not return the DOG SILVER CHAIN

How Can I make the Filtering Dynamic and must accept complex data as such.

Thank You

Use filter_condition_callback. You can see an example in Mage\\Adminhtml\\Block\\Cms\\Page\\Grid . Here's how you use it:

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

Then add the filter function, which breaks up the value into words and adds a condition for each one:

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.'%'));
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM