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.
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');
Let’s create left join on the table in magento.
Let’s create collection array using below code:
$collection = Mage::getModel('event/event')->getCollection();
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();
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; }
This tutorial helps integrate a PHP SDK with Laravel. We'll install aws-php-sdk into laravel application and access all aws services… Read More
in this quick PHP tutorial, We'll discuss php_eol with examples. PHP_EOL is a predefined constant in PHP and represents an… Read More
This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More
We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More
in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More
I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More
View Comments
getSelect will not give you a MAGENTO COLELCTION is a Zend_db... what ever your joining. Has nothing to do with Magento