Magento – join 2 collection in custom Grid Magento

gridjoin-tablemagento-1.8

join 2 collection in custom Grid Magento.I am using the below code. I want to join with sales_flat_order to get get Shipping address & Grand Total. how to do ?

$customerId = $this->getRequest()->getParam('id');//customer Id
$collection = Mage::getModel('modulename/modelname')
    ->getCollection()
    ->addFieldToFilter('customer_id', $customerId);

Best Answer

Please try with this:

$collection
    ->getSelect()
    ->join(
        array('so' => $this->getTable('sales/order')),
        'main_table.order_id = so.entity_id',
        array('so.grand_total')
    )
    ->join(
        array('soa' => $this->getTable('sales/order_address')),
        "(main_table.order_id = soa.parent_id AND soa.address_type = 'shipping')",
        array('soa.*')
    );

replace order_id with the column name that you used in your table for the order ID.

Also you can view/read this - http://www.magentocommerce.com/wiki/1_-_installation_and_configuration/using_collections_in_magento .

Related Topic