Skip to main content
Tweeted twitter.com/StackMagento/status/874873382965317632
Changed tags
Link
sv3n
  • 11.7k
  • 7
  • 41
  • 73
added 2 characters in body; edited tags
Source Link

I need to load one attribute value(weight) for many product IDs.

Say list of the product ids is in array $productIDs$productIDs.

In general case, which approach is faster,

  1. Loading raw attribute value for each product, without loading products:

     foreach ($productIds as $productId) {
         $weight[$productId] = Mage::getResourceModel('catalog/product')->getAttributeRawValue($productId, 'weight', $store_id );
     }
    
  2. Using collection:

     $collection = Mage::getModel('catalog/product')->getCollection()->addAttributeToFilter('id', array('in' => $productIds));
     foreach ($collection as $product) {
         $weight[$product->getId()] = $product->getWeight();
     }
    

I need to load one attribute value(weight) for many product IDs.

Say list of the product ids is in array $productIDs.

In general case, which approach is faster,

  1. Loading raw attribute value for each product, without loading products:

     foreach ($productIds as $productId) {
         $weight[$productId] = Mage::getResourceModel('catalog/product')->getAttributeRawValue($productId, 'weight', $store_id );
     }
    
  2. Using collection:

     $collection = Mage::getModel('catalog/product')->getCollection()->addAttributeToFilter('id', array('in' => $productIds));
     foreach ($collection as $product) {
         $weight[$product->getId()] = $product->getWeight();
     }
    

I need to load one attribute value(weight) for many product IDs.

Say list of the product ids is in array $productIDs.

In general case, which approach is faster,

  1. Loading raw attribute value for each product, without loading products:

     foreach ($productIds as $productId) {
         $weight[$productId] = Mage::getResourceModel('catalog/product')->getAttributeRawValue($productId, 'weight', $store_id );
     }
    
  2. Using collection:

     $collection = Mage::getModel('catalog/product')->getCollection()->addAttributeToFilter('id', array('in' => $productIds));
     foreach ($collection as $product) {
         $weight[$product->getId()] = $product->getWeight();
     }
    
Source Link
vladPavlov
  • 568
  • 2
  • 8
  • 17

What is faster - getting raw attribute value or use collection?

I need to load one attribute value(weight) for many product IDs.

Say list of the product ids is in array $productIDs.

In general case, which approach is faster,

  1. Loading raw attribute value for each product, without loading products:

     foreach ($productIds as $productId) {
         $weight[$productId] = Mage::getResourceModel('catalog/product')->getAttributeRawValue($productId, 'weight', $store_id );
     }
    
  2. Using collection:

     $collection = Mage::getModel('catalog/product')->getCollection()->addAttributeToFilter('id', array('in' => $productIds));
     foreach ($collection as $product) {
         $weight[$product->getId()] = $product->getWeight();
     }