Magento join – EAV and Flat table

Case 1: After the Magento join, we want to have a EAV Collection as a result collection

//the ‘1’ is just a example of a where clause, you can totally miss this part, if you do not need it :)

$collection = Mage::getModel('catalog/product_link')

Case 2: After the Magento join, We want to have Flat table Collection as a result collection

1. we get the attribute id for a specific EAV model and attribute name

$attribute = Model::getModel('eav/entity_attribute')
                    ->loadByCode('customer', 'attribute_name');

2. We get the flat table collection model

$collection = Model::getResourceModel('sales/order_grid_collection')

3. We load the collection with LEFT JOIN on the attribute. In this example we will also join the collection to the order table because we have not customer_id in the order_grid table.

           ->join(array('s' => 'sales_flat_order'),
                  'main_table.entity_id = s.entity_id',
                  array('order_id' => 'entity_id'))
           ->joinLeft(array('attribute_name' => 'customer_entity_varchar'),
                      's.customer_id = attribute_name.entity_id AND 
                       attribute_name.attribute_id = '.
                       array('attribute_name' => 'attribute_name.value')); 

And at the end of both cases you just set the collection to the grid in the ordinary way:


P.S: I did not explain everything really detailed, because I usually do not read the explanations in the articles XD //But if you need something explained, let me know =)