title: Magento Observer to add order grid column - ambiguous issue
body: <p>I have fix the problem, FINALLY!</p>
  
  <p>Basically I'm getting WHERE part with <code>$where = $select-&gt;getPart('where')</code> and then I go through each condition and searching for <code>created_at</code>, If I find matching then I replace <code>created_at</code> with <code>main_table.created_at</code>.</p>
  
  <p>I've tested this, and everything works fine, if there is something that can be "buggy" please let me know.</p>
  
  <p>Thanks all!!</p>
  
  <pre><code>public function salesOrderGridCollectionLoadBefore($observer)
  {
  
    $collection = $observer-&gt;getOrderGridCollection();
    $select     = $collection-&gt;getSelect();
    $select-&gt;joinLeft(array('custab' =&gt; 'my_custom_table'), 'main_table.entity_id = custab.order_id',array('custab.field_to_show_in_grid'));
  
    if ($where = $select-&gt;getPart('where')) {
        foreach ($where as $key=&gt; $condition) {
            if (strpos($condition, 'created_at')) {
                $new_condition = str_replace("created_at", "main_table.created_at", $condition);
                $where[$key] = $new_condition;
            }
        }
        $select-&gt;setPart('where', $where);
    }
  </code></pre>
  
  <p>}</p>
  
  <p>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 <code>created_at</code>.</p>
  
  <p>Problem is because I have same column name (<code>created_at</code>) in custom table and in <code>sales_flat_order_grid</code> table.</p>
  
  <p>I'm getting this error</p>
  
  <blockquote>
    <p>SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'created_at' in where clause is ambiguous</p>
  </blockquote>
  
  <p>If I edit this line <code>'index' =&gt; 'created_at'</code>
   to
   <code>'index' =&gt; '**main_table**.created_at'</code>, in <code>app/code/core/Mage/Adminhtml/Block/Sales/Order/Grid.php</code></p>
  
  <pre><code>$this-&gt;addColumn('created_at', array(
          'header' =&gt; Mage::helper('sales')-&gt;__('Purchased On'),
          'index' =&gt; 'main_table.created_at',
          'type' =&gt; 'datetime',
          'width' =&gt; '100px',
      ));
  </code></pre>
  
  <p>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.</p>
  
  <p>Here is my Observer.php</p>
  
  <pre><code>class Testinggrid_ExtendOrderGrid_Model_Observer{
      public function salesOrderGridCollectionLoadBefore($observer)
      {
         $collection = $observer-&gt;getOrderGridCollection();
         $select     = $collection-&gt;getSelect();
         $select-&gt;joinLeft(array('custab' =&gt; 'my_custom_table'), 'main_table.entity_id = custab.order_id',array('custab.field_to_show_in_grid'));
      }
  }
  </code></pre>
  
  <p>Here is my module layout</p>
  
  <pre><code>&lt;layout&gt;
    &lt;sales_order_grid_update_handle&gt;
      &lt;reference name="sales_order.grid"&gt;
          &lt;action method="addColumnAfter"&gt;
              &lt;columnId&gt;field_to_show_in_grid&lt;/columnId&gt;
              &lt;arguments&gt;
                  &lt;header&gt;Column header&lt;/header&gt;
                  &lt;index&gt;field_to_show_in_grid&lt;/index&gt;
                  &lt;filter_index&gt;field_to_show_in_grid&lt;/filter_index&gt;
                  &lt;type&gt;text&lt;/type&gt;
              &lt;/arguments&gt;
              &lt;after&gt;shipping_name&lt;/after&gt;
          &lt;/action&gt;
      &lt;/reference&gt;
    &lt;/sales_order_grid_update_handle&gt;
    &lt;adminhtml_sales_order_grid&gt;
      &lt;!-- apply layout handle defined above --&gt;
      &lt;update handle="sales_order_grid_update_handle" /&gt;
    &lt;/adminhtml_sales_order_grid&gt;
    &lt;adminhtml_sales_order_index&gt;
      &lt;!-- apply layout handle defined above --&gt;
      &lt;update handle="sales_order_grid_update_handle" /&gt;
    &lt;/adminhtml_sales_order_index&gt;
  &lt;/layout&gt;
  </code></pre>