Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to join collections in Magento?

I am trying to join a custom collection with products to show the product name (not just the id) in the admin grid widget. So far i can't find the correct syntax.

I am able to retrieve the products with the product name by the following:

Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('name');

and i can retrieve my custom collection with:

Mage::getResourceModel('xyz_mymodule/model_collection');

How do i join the two so that the module collection is the primary collection and the id as returned by $model->getId() is still the id of my custom collection?

like image 871
user2683224 Avatar asked Aug 14 '13 16:08

user2683224


People also ask

What are collections in Magento 2?

Collections are encapsulating sets of models and related functionality, such as filtering, sorting, and paging. A collection in Magento is a class that implements both the IteratorAggregate and the Countable PHP5 SPL interfaces.

How do I run a custom query in Magento 2?

You need to use the $this->resourceConnection object to run any Direct SQL Query. Reading From The Database All the records, fetchAll() => This method Fetches all SQL result rows as a sequential array. This method takes a query as it's the first parameter, executes it, and then returns all of the results as an array.

What is join in mysql?

A JOIN clause is used to combine rows from two or more tables, based on a related column between them.


1 Answers

Here you have a working 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'));
like image 182
Beto Castillo Avatar answered Nov 03 '22 09:11

Beto Castillo