How to Load Model data joining 2 tables?

Inside resource model class add following function

protected function _getLoadSelect($field, $value, $object)
{
    $select = parent::_getLoadSelect($field, $value, $object);

    $select->joinLeft(
        array('t_b' => 'table_b'),
        $this->getMainTable() . '.id = t_b.id',
        array('columnA'));
    return $select;
}

When you called Mage::getModel('modulename/TableA')->load(1), this method will join with table_b.


I can propose you the approach that I use, and it is quite easy.

Steps:

  1. Rewrite resource model of Model A (If Model A is yours model and not Magento then you can skip this step)

  2. Rewrite method _getLoadSelect() of resource model of Model A. Select query is generated in this method and here you can join your the other tables.

Tags:

Sql

Model