Tag Archives: Magento collections

  • Find Out The SQL Query That Was Used To Retrieve A Collection

    Posted on August 4, 2011 by admin

    To find out the SQL query Magento uses to retrieve a specific collection, just do the following:

    Let's say you would like to know what query is used to get a product collection - go to your debugging area (or maybe some phtml file), and paste following code:


    $_productCollection = Mage::getResourceModel("catalog/product_collection");


    Passing "true" as a parameter to the load function, will print out the query that Magento uses to retrieve the items for the collection.

    This post was posted in Working With Magento Collections and was tagged with Magento collections, debugging

  • Changing The Select After The Collection Has Been Loaded

    Posted on August 4, 2011 by admin

    Lets observe at the following example:

    For some reason you want to show the products whose price is lower than 100$.
    If you go to list.phtml at app/design/frontend/base/default/template/catalog/product, you will see the following line:


    $_productCollection is instance of Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection, and of course this is already loaded collection (collection that has items). resource collections in Magento - once the collection is loaded, you cannot add anything to the "select" and then call "load" again - this will not change the items of a collection. The solution is:

    $_productCollection->getSelect()->where('price_index.price < 100'); $_productCollection->load();

    In line 1 we called to "clear" function that deleted all items in the collection, but preserved the current select. So in the line 2 we added stuff to the select, and called "load". This gave us the desired result.

    This post was posted in Working With Magento Collections and was tagged with Magento collections

2 Item(s)

Magento is a well-engineered eCommerce platform designed to help engineers develop customized eCommerce online stores. Due to lack of proper coding documentation, Engineer-ing.com was created with the sole purpose of instructing Magento developers to-be with the "how-to-do" know-how. In the event of unresolved issues, you are more than welcome to contact me for consultation. However, please do so only if you possess a Software Engineering background and you're able to specify your question.