Posted about 4 years ago. Visible to the public.

Admin Grids - Filtering A Joined Column

Often, you'll want to add a column to a collection used in a grid, and to do so you'd use _prepareCollection():

Copy
protected function _prepareCollection() { $collection = Mage::getResourceModel($this->_getCollectionClass()); $this->setCollection($collection); $collection->getSelect() ->joinLeft( array('table_alias' => 'some_long_table_name'), 'main_table.something = table_alias.something', array('some_column' => 'table_alias.some_column') ); return Mage_Adminhtml_Block_Widget_Grid::_prepareCollection(); } protected function _prepareColumns() { parent::_prepareColumns(); $this->addColumnAfter( 'some_column', [ 'header' => Mage::helper('sales')->__('Some Column'), 'index' => 'some_column', ], 'add_after_column' ); }

This works fine until you try and filter the grid, at which point you'll get an unknown column. The issue is that Magento doesn't know what column some_column is, so it just refers to it in the SQL statement as some_column, which doesn't work, because column aliases don't work in WHERE conditions.

To fix the problem, you need to tell Magento what 'real' column it is in the collection prepation, like so:

Copy
protected function _prepareCollection() { $collection = Mage::getResourceModel($this->_getCollectionClass()); $this->setCollection($collection); $collection->getSelect() ->joinLeft( array('table_alias' => 'some_long_table_name'), 'main_table.something = table_alias.something', array('some_column' => 'table_alias.some_column') ); $collection->addFilterToMap('some_column', 'table_alias.some_column'); return Mage_Adminhtml_Block_Widget_Grid::_prepareCollection(); }

Thanks to this post for the tip.

Owner of this card:

Avatar
Mike Whitby
Last edit:
about 4 years ago
by Mike Whitby
Posted by Mike Whitby to Magento
This website uses cookies to improve usability and analyze traffic.
Accept or learn more