Friday, 9 August 2013

Magento Observer to add order grid column - ambiguous issue

Magento Observer to add order grid column - ambiguous issue

I'm trying to add new column in sales order grid from custom table using
observer. Everything works fine until I try to filter grid using column
created_at.
Problem is because I have same column name (created_at) in custom table
and in sales_flat_order_grid table.
I'm getting this error
SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'created_at'
in where clause is ambiguous
If I edit this line 'index' => 'created_at' to 'index' =>
'main_table.created_at', in
app/code/core/Mage/Adminhtml/Block/Sales/Order/Grid.php
$this->addColumn('created_at', array(
'header' => Mage::helper('sales')->__('Purchased On'),
'index' => 'main_table.created_at',
'type' => 'datetime',
'width' => '100px',
));
everything work fine, but I don't want to change core files or to copy
them to local folder and edit, I think that there is some simple solution
that I need to add to my observer.
Here is my Observer.php
class Testinggrid_ExtendOrderGrid_Model_Observer{
public function salesOrderGridCollectionLoadBefore($observer)
{
$collection = $observer->getOrderGridCollection();
$select = $collection->getSelect();
$select->joinLeft(array('custab' => 'my_custom_table'),
'main_table.entity_id =
custab.order_id',array('custab.field_to_show_in_grid'));
}
}
Here is my module layout
<layout>
<sales_order_grid_update_handle>
<reference name="sales_order.grid">
<action method="addColumnAfter">
<columnId>field_to_show_in_grid</columnId>
<arguments>
<header>Column header</header>
<index>field_to_show_in_grid</index>
<filter_index>field_to_show_in_grid</filter_index>
<type>text</type>
</arguments>
<after>shipping_name</after>
</action>
</reference>
</sales_order_grid_update_handle>
<adminhtml_sales_order_grid>
<!-- apply layout handle defined above -->
<update handle="sales_order_grid_update_handle" />
</adminhtml_sales_order_grid>
<adminhtml_sales_order_index>
<!-- apply layout handle defined above -->
<update handle="sales_order_grid_update_handle" />
</adminhtml_sales_order_index>
</layout>

No comments:

Post a Comment