I am interested in the exact process of getting the values for a product collection.

If I have a product collection

$products = Mage::getModel('catalog/product')->getCollection();

it will just provide basic product attributes like sku.

Example from a $product->debug()

    [entity_id] => 3221
    [entity_type_id] => 4
    [attribute_set_id] => 9
    [type_id] => simple
    [sku] => 12345678
    [has_options] => 1
    [required_options] => 1
    [created_at] => 2015-01-01 15:20:27
    [updated_at] => 2015-02-27 11:24:49
    [cat_index_position] => 20009
    [is_salable] => 1
    [stock_item (Varien_Object)] => Array
            [is_in_stock] => 1


When I need more values I can use addAttributeToSelect() to extend the information I get from my collection. E.g. I mostly use


because it is not the "I don't care, just gimme everything" hammer like '*' and mostly fits my needs and it works very well. The $product->debug() contains much more values after adding this.

What I don't understand is the mechanism behind it. When I use $products->load(false,true) to dump the sql, there is no hint to the extra attributes.

SELECT `e`.*, `cat_index`.`position` AS `cat_index_position` 
FROM `catalog_product_entity` AS `e`
INNER JOIN `catalog_category_product_index` AS `cat_index` 
  ON cat_index.product_id=e.entity_id 
    AND cat_index.store_id=1 
    AND cat_index.visibility IN(2, 4) 
    AND cat_index.category_id = '2' 
ORDER BY `e`.`entity_id` ASC

I went down to \Mage_Eav_Model_Entity_Collection_Abstract::addAttributeToSelect but I am not sure where to go from there.

Somewhere between getCollection() and a foreach($products as $product) the values are added and I really would like to know where and how the magic happens :)

Can somebody explain this to me?

And to extend my question: Where are the borders of the addAttributeToSelect() function? It seems it can do some joining but where is the point you need to do it yourself with joinAttribute()? Hope I could make myself clear with this.

Thanks in advance! :)

1 Answer 1


Using the collection in a foreach loop causes the collection to be loaded (see Varien_Data_Collection for which functions trigger load). The load function of Varien_Data_Collection is overloaded by Mage_Eav_Model_Entity_Collection_Abstract, which is part of the inheritance chain of the product collection (Mage_Catalog_Model_Resource_Product_Collection). Somewhere in the middle of the load method it triggers Mage_Eav_Model_Entity_Collection_Abstract::loadAttributes which is responsible for hydrating all the attribute data.

Your Answer

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

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