2

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

1 Answer 1

2

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

16
  • 1
    @BrittanyMoore check updated code Sep 21, 2017 at 11:58
  • 1
    ahh I got it array('supplyNeeds.created_at as ocreated' ) because of this code has issue replcae with field of your table, in array put all comma separted fields that you want in select or just do array('supplyNeeds.* ') Sep 21, 2017 at 12:29
  • 1
    I updated the code please check Sep 21, 2017 at 12:31
  • 1
    print query and check in phpmyadmin Sep 21, 2017 at 12:55
  • 1
    You can do groupby Sep 22, 2017 at 9:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.