2017-10-30 7 views
0

Products Moduleの列名をフィルタリングするクエリがあります。ここでZend Framework MagentoでjoinFieldのwhere like queryを取得する方法

は、列

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

どのようにフィルタリングを動的にすることができますか?複雑なデータをそのまま受け入れる必要があります。

はあなたに

答えて

0

使用filter_condition_callbackをありがとうございます。例はMage\Adminhtml\Block\Cms\Page\Gridにあります。ここでは、それを使用する方法は次のとおりです。

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

そして、言葉に値を分割し、それぞれの条件を追加するフィルタ機能を追加:

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.'%')); 
    } 
} 
関連する問題