1

Why does my attribute return only "No" (even though some products have the attribute set to "Yes") when I try to access it from a collection, but does return "Yes" or "No" appropriately I load the product individually?


$attributes = array('my_attribute','another_attribute');

$productsCollection = Mage::getModel('catalog/product')
    ->getCollection()
    ->addAttributeToSelect($attributes);
$productsCollection->load();

foreach ($productsCollection as $_product) {
    $value = $_product->getAttributeText('my_attribute'); // only returns "No";
}

I can get it to return the proper value if I reload the product individually, but that's way too inefficient for my needs.

$attributes = array('my_attribute','another_attribute');

$productsCollection = Mage::getSingleton('catalog/product')
    ->getCollection()
    ->addAttributeToSelect($attributes);
$productsCollection->load();

foreach ($productsCollection as $_product) {
    // Reload product individually
    $_product = Mage::getSingleton('catalog/product')
                ->load($_product->getId());
    $value = $_product->getAttributeText('my_attribute');
    //return "Yes" and "No" appropriately;
}

How can I efficiently, grab "select" attribute values for thousands of products?

1

try this..

$prod = Mage::getModel('catalog/product')->load($productId);
$value = $prod->getResource()->getAttribute('my_attribute')->getFrontend()->getValue($prod);
echo $value;

When use Product flat setting enable:

Table:

product  has been load from `catalog_product_flat_STOREID` table.

load(), product is load from product master table catalog_product_entity with join with multiple models,modules.

Attributes:

You can only get `few product attributes values Whose setting is enable for Use in Product listing.

load():get all attributevalues of a particular product with inventory prices,tax etc in full details.`


Speedup:

load() surely slow because multiple eav tables join,multiple module join like tax,inventory etcs When disable product Flat setting:


Table:

product hasbeen load from catalog_product_entity table and join multiple eav,products eav using function likeaddAttributeToSelect,addAttributeToSort,addAttributeToFilter()` etc.

load() same AS whenever flat is enable

Attributes:

You can get only those attribute value whose are select via addAttributeToSelect.,addUrlRewriteToResult() etc.`

load(): get all attribute values of a particular product with inventory prices, tax etc in full details.

Speedup:

Totaly depend on attribute selection and join product t related models`.

load() surely slow because multiple eav tables join, multiple module join like tax, inventory etc.

As per my point view ,it totally depends on attribute selection,filter etc of product. If your attribute value is filled collection then you need to use collection.

Otherwise use Product model load. Because of collection is not enable giving all data

  • would this not have the issue of being inefficient, since it loads every product, as opposed to having it pre-grabbed at the collection level? – ahnbizcad Oct 13 '16 at 19:19
  • No, 'Mage::getModel('catalog/product')->load()' is object of Magento product class Mage_Catalog_Product_Model and $collection->getFirstItem() is object of Magento product collection class Mage_Catalog_Product_Model_Resource_Product_Collection or Mage_Catalog_Model_Resource_Category_Flat_Collection ok let me expain in another answer it 's depends on magento flat setting – lalit mohan Oct 13 '16 at 19:29
  • I tried running this code, and it returns only "No". It does not grab the correct values of the attribute – ahnbizcad Oct 14 '16 at 15:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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