How to join collections in Magento?

这一生的挚爱 提交于 2020-01-11 18:50:38

问题


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?


回答1:


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'));

Greetings, hope it helps.




回答2:


Just a quick correction I had to add quotes on the table name: array('order_item'=> 'sales_flat_order_item'), Also getSelect() is not necessary as the third argument is the attribute list. Final argument specifies the type of join you would like to use.

My version looked like this:


$collection = Mage::getResourceModel($this->_getCollectionClass());
$collection->join(array('order'=> 'sales/order'),'order.entity_id=main_table.entity_id', array('po_number'=>'po_number', 'imi_customer_email' =>'imi_customer_email'), null,'left');
$this->setCollection($collection);`


来源:https://stackoverflow.com/questions/18237348/how-to-join-collections-in-magento

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!