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.
main_table
.*,c
.* FROMsales_flat_order_grid
ASmain_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 BYsales_flat_order_status_history
.parent_id
DESC) ASc
ON c.parent_id = main_table.entity_id