Magento status and created_at grid view issues

Have you ever tried to change the Magento Grid View collections to a custom selects and joins?

For example you want your Invoice Grid to take some columns from the Order table:

 


From:

$collection = Model::getResourceModel('sales/order_invoice_grid_collection');

To:


$collection = Mage::getResourceModel('sales/order_invoice_grid_collection')
                    ->addAttributeToSelect('*');

$collection->getSelect()->join(array('s' => 'sales_flat_order'),
                               'main_table.order_id = s.entity_id',
                               array('order_id' => 'entity_id'));

In this example everything is working properly on first sight but then if try to use the filter for columns which can be found in several tables you have been:

1. Redirected to the Dashboard
2. Introduced to the following error:

What can you do?

1. Check if you really need all tables that you join – I did the same thing in the Order Grid and I was joining the sales_flat_order_grid and the sales_flat_order – lame I know 😀
2. If you really need all of them add a ‘filter_index’ => ‘main_table.column_name’ to all common named columns

For the Invoice Grid joined with Order the columns ‘state’ and ‘created_at’ as common and the code for them should be changed:

From:


$this->addColumn('state', array('header'  => Mage::helper('sales')->__('Status'),
                                'index'   => 'state',
                                'type'    => 'options',
                                'options' => Mage::getModel('sales/order_invoice')
                                                   ->getStates()));

To:


$this->addColumn('state', array('header'       => Mage::helper('sales')->__('Status'),
                                'index'        => 'state',
                                'type'         => 'options',
                                'filter_index' => 'main_table.state',
                                'options'      => Mage::getModel('sales/order_invoice')
                                                        ->getStates()));

Hope that it helps =)