Tutorialsplane

Magento join two custom tables


Magento join two custom tables : You can join two custom tables in magento as below.

Magento join two custom tables Syntax

Following syntax is used for joining two tables in magento :

//load collection 
$collection = Mage::getModel('module/model_name')->getCollection();
$collection->getSelect()->join(Mage::getConfig()->getTablePrefix().'table_to_be_joined','main_table.table_field ='.Mage::getConfig()->getTablePrefix().'table_to_be_joined.table_field',array('field_name'));

Magento join two custom tables Example

$collection = Mage::getModel('sales/order')->getCollection();
$collection->getSelect()->join( array('order_item'=> 'sales_flat_order_item'), 'order_item.order_id = main_table.entity_id', array('order_item.sku'));