2

I'm working with custom module for the magento backend, in this filter not working when using filter calback! Can anyone suggest me? Thanks!

I have tried some codes like this,

Grid.php

protected function _prepareCollection()
    {
     $collection = Mage::getModel('listings/listings')->getCollection();
     $this->setCollection($collection);
       return parent::_prepareCollection();
    }
protected function _prepareColumns()
    {     
        $this->addColumn("item_id", array(
            "header" => Mage::helper("linkmanagement")->__("ID"),
            "align" => "center",
            "type" => "number",
            "index" => "item_id",

        ));

        $this->addColumn("title", array(
            "header" => Mage::helper("linkmanagement")->__("Title"),
            "index" => "title"
        ));

        $this->addColumn("cat_ids", array(
            "header" => Mage::helper("linkmanagement")->__("Cat ID"),
            "align" => "center",
            "index" => "cat_ids",
            "renderer" => 'Sathish_Linkmanagement_Block_Adminhtml_Linkmanagement_Renderer_Categories',
            'filter_condition_callback' => array($this, '_categoriesFilter')
        ));

        $this->addColumn("url_key", array(
            "header" => Mage::helper("linkmanagement")->__("URL"),
            "index" => "url_key",
            "width" => "200px",
        ));

        $this->addColumn('status',
            array(
                'header' => Mage::helper('linkmanagement')->__('Status'),
                'index' => 'status',
                'type' => 'options',
                'options' => array('1' => Mage::helper('linkmanagement')->__('Active'),
                    '0' => Mage::helper('linkmanagement')->__('Inactive')),
            )
        );

        return parent::_prepareColumns();
    }

callback function

protected function _categoriesFilter($collection, $column)
    {
        if (!$value = $column->getFilter()->getValue()) {
            return $this;
        }

        $this->getCollection()->getSelect()->where(
            "cat_ids ?"
            , "%$value%");
        return $this;
    }

Categories.php

class Sathish_Linkmanagement_Block_Adminhtml_Linkmanagement_Renderer_Categories extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
{

    public function render(Varien_Object $row)
    {
        $value =  $row->getData($this->getColumn()->getIndex());
        // $value = 38,92
        $value = explode(',',$value);
        $collection = Mage::getModel("categories/categories")->getCollection()->addFieldToFilter('category_id',$value)->getData();
        foreach($collection as $col){
            $result[] = $col['title'];
        }
        $result = implode(',', $result);// $result = schools,colleges
        return $result;
    }
}

Note: Renderer working fine., only filter callback function is not working!!!

Sathish
  • 2,440
  • 1
  • 11
  • 27
  • Define not working. Is the callback ever being called? Is the callback code having no affect on the results? – Lee Saferite Jul 07 '15 at 13:20
  • "not working" when ever I tried to filter that column its not return the filter rows – Sathish Jul 07 '15 at 13:26
  • What is the SQL being generated when using your callback then? If, when you apply the callback filter, you get no results then perhaps your query is wrong. Given the information you've posted it's kinda hard to give an answer. – Lee Saferite Jul 07 '15 at 14:03

1 Answers1

3

I checked your example with my own renderer and tried to check your callback.

Replace in your callback method

$this->getCollection()->getSelect()

with

$collection

Here are my pieces of advice:


Magento filter assumes not 100% similarity. I mean that if you want to apply filter you should use construction:

where("cat_ids LIKE ?", "%$value%");

I still don't understand answer on question below:

Define not working. Is the callback ever being called? Is the callback code having no affect on the results? – Lee Saferite 1 hour ago

Put Mage::log('blabla', false, 'grid.log', true); at the beginning of the callback method. Then check this log file. If it's not empty - your method calls successfully.


If your method calls - try to

Mage::log($collection->getSelectSQL(1), false, 'grid.log', true);

before and after filter applying. And try to run these queries in phpmyadmin. Check the result


Try to apply these changes inside Mage_Adminhtml_Block_Sales_Order_Grid class

Here what I did:

    $this->addColumn("cat_ids", array(
        "header" => Mage::helper('sales')->__('Ship to Name'),
        "align" => "center",
        "index" => "grand_total",
        "renderer" => "My_Class_.....",
        "filter_condition_callback" => array($this, '_categoriesFilter')
    ));

....

protected function _categoriesFilter($collection, $column)
{
    if (!$value = $column->getFilter()->getValue()) {
        return $this;
    }

    $collection->getSelect()->where(
        "status LIKE ?", "%$value%"
    );

    return $this;
}

...

public function render(Varien_Object $row)
{
    $value =  $row->getData('increment_id') . $row->getData('status');
    return $value;
}
zhartaunik
  • 932
  • 12
  • 29
  • if I use `$collection` instead of `$this->getCollection()->getSelect()` shows a `Fatal error: Call to undefined method Sathish_Listings_Model_Mysql4_Listings_Collection::where()` – Sathish Jul 08 '15 at 04:13
  • I use two models "listings/listings" and "categories/categories" – Sathish Jul 08 '15 at 04:25
  • If you diff my and your code - you will see, that you send $collection to your function and never use it. You apply filter to some another collection, which doesn't use. I'm not sure, but I think your error appear because you didn't make propper inheritance. And your collection doesn't have Zend methods. And your issue doesn't refer to callback function. You should extend Mage_Core_Model_Mysql4_Collection_Abstract. Maybe try to follow my last advice. When your callback start working - you can transfer it part by part to your module. Variant, which I post is 100% workable. – zhartaunik Jul 08 '15 at 04:27