Magento: join with magento collection

In this magento article, We will discuss how to use join in magento. The magento worked on collections based on module.

A collection is a Model type entity that containing other Models. We will use getCollection() method to load event collection.

We will create join between table and fetch data from table. The Collections in magento is like an array of model object but which many more features. You can apply sql query into collection object and filter out result data.

Loading Product Collection in Magento

There are two ways of loading product collection:

Call getCollection() method in the product model instance:

$collection = Mage::getModel('catalog/product')->getCollection();

Load collection class in the getResourceModel() factory method:

$collection = Mage::getResourceModel('catalog/product_collection');

Create Left Join in Magento

Let’s create left join on the table in magento.

Let’s create collection array using below code:

$collection = Mage::getModel('event/event')->getCollection();

How To Get Database Table

We can get database table with help of below code:

$msa_eventType = Mage::getSingleton('core/resource')->getTableName('event_type');

Now, We have collection and table where we want to add join, So below code will create a LEFT JOIN with ‘event type’ table.

$collection->getSelect()->joinLeft(array('event_type'=>$msa_eventType),'`main_table`.`type_id` = `event_type`.`type_id`',array('type_name'));

As we can see above code, I have created new collection which has data based on our join criteria. Let’s fetch all data from collections using below code.

$arrData=$collection->getData();

Full Code of join with magento collection:

function getEventCollection($eventId)  
    {  
        //create the collection array for Event management  
        $collection = Mage::getModel('event/event')->getCollection();  
        // get database table   
        $msa_eventType = Mage::getSingleton('core/resource')->getTableName('event_type');      
        //make left join to type  
        $collection->getSelect()->joinLeft(array('event_type'=>$msa_eventType),'`main_table`.`type_id` = `event_type`.`type_id`',array('type_name'));  
        // get database table   
        $msa_eventLocation = Mage::getSingleton('core/resource')->getTableName('event_location');      
        //make left join to location  
        $collection->getSelect()->joinLeft(array('event_location'=>$msa_eventLocation),'`main_table`.`loc_id` = `event_location`.`loc_id`',array('loc_name'));  
       // check if event id is empty then get full event list  
       if(emptyempty($eventId))  
       $arrData=$collection->getData();  
       else  
       //get a particular event  
       $arrData=$collection->addFilter('event_id',$eventId)->getData();  
       //return data  
       return  $arrData;  
    }

One thought on “Magento: join with magento collection

  1. getSelect will not give you a MAGENTO COLELCTION is a Zend_db… what ever your joining. Has nothing to do with Magento

Leave a Reply

Your email address will not be published. Required fields are marked *