Skip to content

Group by on collection on Magento 1.7

In Magento 1.7, I added a column in the shipment grid displaying the total of the associated order for each shipment. To achieve this, in the Mage_Adminhtml_Block_Sales_Shipment_Grid file, I added the following code:

protected function _prepareCollection()
{
    $collection = Mage::getResourceModel($this->_getCollectionClass());
    $collection->getSelect()->joinLeft(
        array('order' => $collection->getTable('sales/order')),
        'order.entity_id = main_table.order_id',
        array()
    );
    $collection->addExpressionFieldToSelect('importo', 'SUM({{base_grand_total}})', array('base_grand_total' => 'base_grand_total'));
    $collection->getSelect()->group('main_table.entity_id');

    $collection->addFilterToMap('created_at', 'main_table.created_at'); //resolves ambiguity in column names
    $this->setCollection($collection);
   // echo $collection->getSelect()->__toString();die();
    return parent::_prepareCollection();
}

The “group by” statement generates an incorrect query (it returns one row for each shipment). How can I resolve this? Ideally, I would like to add the group('main_table.entity_id') only for the grid, not for getSize.