Magento 1.9 EAV – Join EAV Attributes

eavgridjoin;magento-1.9products

Im trying to join attributes in my collection on my grid view of "supply needs" from my ERP (Erp boost my shop)

The collection is loaded like this:

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

And then i tried to join it with catalog/product on product_id, which works fine, but it doesnt give me the attribute "product_class" that i want, because it tries to load it from purchase/supplyneeds.

I have joined it like this:

$collection->join('catalog/product', 'product_id=entity_id');

Which gives me a collection that looks like this for each product:

[product_id] => 8728
[manufacturer_id] => 570
[sku] => 18369
[name] => NEW - Tiny Teasers Opladelig Bullet Vibrator
[waiting_for_delivery_qty] => 0
[status] => 3_prefered_stock_level
[qty_min] => 0.0000
[qty_max] => 1.0000
[entity_id] => 8728
[entity_type_id] => 4
[attribute_set_id] => 17
[type_id] => simple
[has_options] => 0
[required_options] => 0
[created_at] => 2017-08-22 14:01:37
[updated_at] => 2017-08-22 14:04:17
[exclude_from_supply_needs] => 0

But how do i get my attribute product_class in my collection? a foreach takes too much time to load for 600 products

The getSQL looks like this with join statement:

SELECT `main_table`.*, `catalog/product`.* FROM `erp_view_supplyneeds_global` AS `main_table`
 INNER JOIN `catalog_product_entity` AS `catalog/product` ON product_id=entity_id

Best Answer

create main collection of product and join your table with it

 $collection =  Mage::getModel('catalog/product')->getCollection();
 $collection->join('Purchase/SupplyNeeds', 'product_id=entity_id');
 $collection->addAttributeToFilter("product_class",array('neq'=>''));

Or try Below code

 $connection = Mage::getSingleton('core/resource');

 $coll = Mage::getModel('catalog/product')->getCollection()
                 ->addAttributeToFilter('product_class',array('neq'=>''));
 $coll->getSelect()->join( array('supplyNeeds'=> $connection->getTableName('Purchase/SupplyNeeds')), 'supplyNeeds.product_id = e.entity_id', array('supplyNeeds.*' ) ); // here in last parameter pass all fields that you need from your table

Try above code and let me know