Magento Sales Order Grid Joining Table
I am wondering if anyone out there has had much luck joining the order items table to the sale order grid and being able to filter correctly?
I have already been able to complete the join, like so:
protected function _prepareCollection()
{
parent::_prepareCollection();
$collection = Mage::getResourceModel($this->_getCollectionClass())
->join(
'sales/order_item',
'`sales/order_item`.order_id=`main_table`.entity_id',
array(
'skus' => new Zend_Db_Expr('group_concat(`sales/order_item`.sku SEPARATOR ",")'),
'names' => new Zend_Db_Expr('group_concat(`sales/order_item`.name SEPARATOR ",")'),
)
);
$collection->getSelect()->group('entity_id');
$this->setCollection($collection);
return Mage_Adminhtml_Block_Widget_Grid::_prepareCollection();
}
But I am now trying to add the column filter like so:
protected function _addColumnFilterToCollection($column)
{
if($this->getCollection() && $column->getFilter()->getValue())
{
if($column->getId() == 'skus')
{
$this->getCollection()->join(
'sales/order_item',
'`sales/order_item`.order_id=`main_table`.entity_id',
array(
开发者_Python百科 'skus' => new Zend_Db_Expr('group_concat(`sales/order_item`.sku SEPARATOR "|")'),
)
)->getSelect()
->group('`main_table`.entity_id')
->having('find_in_set(?, `main_table`.skus)', $column->getFilter()->getValue());
return $this;
}
if($column->getId() == 'names')
{
$this->getCollection()->join(
'sales/order_item',
'`sales/order_item`.order_id=`main_table`.entity_id',
array(
'names' => new Zend_Db_Expr('group_concat(`sales/order_item`.name SEPARATOR ",")'),
)
)->getSelect()
->group('`main_table`.entity_id')
->having('find_in_set(?, names)', $column->getFilter()->getValue());
return $this;
}
}
return parent::_addColumnFilterToCollection($column);
}
Each time I try to filter I get this error: "Column not found: 1054 Unknown column 'main_table.names' in 'having clause...".
Please let me know if you have experienced this before and it you have any pointers?
I had the same problem, when i took al closer look at the problem I did see that the pagination query was causing the error. Alter the getSelectCountSql() in your model, add the following
$countSelect->reset(Zend_Db_Select::HAVING);
Your page will be shown, but you have to alter the getSelectCountSql() tot get the correct number of pages and results.
精彩评论