LinkedIn Twitter RSS Reset

Creating Collection of Objects in Magento using a "Magical Class":Varien_Data_Collection

Have you ever wondered how Collection in Magento are built, containing array of individual Entity Objects of Magento? Collections are one of the most important data structure (if I am right) used in Magento. Most of the queries in Magento will result into collection, and then you will apply a foreach loop to access individual Entity Objects and their values. Today I came into a different situation where I had individual objects but needed to create a collection of those objects, so that I could return the collection from a function.

I have not drilled down to the details of collection, untill today when I really need it! Do you think its very late? because I have been into Magento for more than a year now!. Better late than never! So, I got the way of working out my situation. Here is how I created a collection from Magento’s “Magical Class” Varien_Data_Collection. I call this “Magical Class”, because it worked like a Magic to me :D

$collection=new Varien_Data_Collection();
$collection->addItem($objProduct);

There are various other functions in this class you might need to “work around” a problem. So, have a look and certainly do comment if you also think this class to be a Magical Class!

6 Responses to “Creating Collection of Objects in Magento using a "Magical Class":Varien_Data_Collection”

  1. March 26, 2010 at 7:14 pm #

    This looks useful. Where is $objProduct coming from though? And do you need to add each object individually back into the new collection? Perhaps you could explain in a little more detail when you have the time! Thanks.

  2. March 27, 2010 at 12:17 am #

    Thanks very much Subesh!

  3. kasey mccurdy
    April 14, 2010 at 4:08 pm #

    There are 3 “L’s” in _Colllection — it should be _Collection

    • Subesh Pokhrel
      April 15, 2010 at 4:06 am #

      Thanks!

  4. August 8, 2011 at 3:49 pm #

    Can you edit your post to correct the triple ‘L’ ?

    Colllection -­> Collection

    thx

    • admin
      August 19, 2011 at 4:39 am #

      Thank you for pointing out the typo!

Leave a Comment