Search code examples
magentocollectionsadminhtml

create custom collection in magento


We have a table with delivery dates and i want to add it to the adminhtml in sales/order/grid. The problem that i have is that the left join examples dont work for me and im out of options.

Mage/Adminhtml/Block/Sales/Order/Grid.php

The method im trying now is to create a custom query in _prepareCollection()

    $resource = Mage::getSingleton('core/resource');
    $readConnection = $resource->getConnection('core_read');

    $query = 'SELECT * FROM mage_sales_flat_order_grid
              left join mage_aw_deliverydate_delivery
              on mage_sales_flat_order_grid.entity_id =  
              mage_aw_deliverydate_delivery.order_id';

    $collection = $readConnection->fetchAll($query);

   //$collection = Mage::getResourceModel($this->_getCollectionClass());
   //$this->setCollection($collection);
   return parent::_prepareCollection();

Then i add this column code in _prepareColumns()

    $this->addColumn('delivery_date', array(
        'header' => 'delivery_date',
        'index' => 'delivery_date',
        'type' => 'datetime',
        'width' => '100px',
    ));

I dont even see the new column when i look at the orders grid in the backoffice.

Any help is appreciated!


Solution

  • If you want to add the anthor table

    protected function _prepareCollection()
        {
            $collection = Mage::getResourceModel($this->_getCollectionClass());
    
           $collection->getSelect()->joinLeft('aw_deliverydate_delivery', 'main_table.entity_id = aw_deliverydate_delivery.order_id',array('*' ) );
            $this->setCollection($collection);
            return parent::_prepareCollection();
        }
    

    And the below code in _prepareColumns()

     $this->addColumn('aw_deliverydate_delivery', array(
                'header' => Mage::helper('sales')->__('Delivery Date '),
                'index' => 'aw_deliverydate_delivery',
                'type' => 'datetime',
                'width' => '100px',
    
            ));