0

I have created a custom Grid in the adminpanel where i need to show orders and the last comment for the order. I did this by adding Join in the _prepareCollection function and the data is showing properly.

protected function _prepareCollection()
    {
        $collection = Mage::getResourceModel($this->_getCollectionClass());
        $subquery = new Zend_Db_Expr('(SELECT parent_id,MAX(entity_id) as comment_id,created_at as comment_date,comment FROM sales_flat_order_status_history GROUP BY parent_id ORDER BY `sales_flat_order_status_history`.`parent_id` DESC)');
        $collection->getSelect()->joinLeft(array('c'=>$subquery), 'c.parent_id = main_table.entity_id', array('c.*'));
        //echo $collection->getSelect();exit;
        $this->setCollection($collection);
        return parent::_prepareCollection();
    }

However if i try to filter the comment column its showing following error.

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'comment' in 'where clause'

What i'm missing. Please help me to resolve this.

6
  • would you give me final query with comment filter ?? Mar 1, 2017 at 5:57
  • Do you want me to print the raw query? This is what i get when i print it SELECT main_table.*, c.* FROM sales_flat_order_grid AS main_table LEFT JOIN (SELECT parent_id,MAX(entity_id) as comment_id,created_at as comment_date,comment FROM sales_flat_order_status_history GROUP BY parent_id ORDER BY sales_flat_order_status_history.parent_id DESC) AS c ON c.parent_id = main_table.entity_id Mar 1, 2017 at 6:01
  • above query working fine for me Mar 1, 2017 at 6:04
  • provide code and query that you are trying for filter in comment Mar 1, 2017 at 6:14
  • This is the query that i get when i do echo echo $collection->getSelect(); in _prepareCollection(); But i think the actual error is triggered in this file /lib/Varien/Db/Statement/Pdo/Mysql.php Mar 1, 2017 at 6:15

1 Answer 1

2

Found a solution for the issue. We need to call custom function for filter for custom columns This is done by as follows.

$this->addColumn('comment', array(
            'header' => Mage::helper('sales')->__('Comment'),
            'index' => 'comment',
            'filter_condition_callback' => array($this,'filterComment'),
        ));

public function filterComment($collection, $column) {

        if (!$value = $column->getFilter()->getValue()) {
            return $this;
        }
        $collection->getSelect()->where(
        "c.comment like ?", "%$value%");
        return $this;
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.