This is maybe one of those things that I end up researching almost every time that I need to understand what is happening under the Magento collection’s ‘hood’. So, I decided to make a short post out of it as a mental note and as a note for anyone searching for the same thing. It is in fact quite simple. Let’s review:
A collection in Magento is a robust array of objects. I say robust because it has a lot more meta data than one would normally expect which describes the cache, type, attributes of the collection and actually much more. This is inherently so because Magento runs on top of the infamous Zend Framework. Additionally, some collections, like the product collection, are not normal translation of database records but a complex set of joins of several tables that represent these products that build this collection. This is due to the good n’ bad EAV model which Magento uses. It is great because it is very flexible, but it is bad because it is complex and slow.
Anyway, long story short: in order to understand what happens under the ‘hood’ of the collection in Magento I often find myself in need of printing the collection’s select query statement in various stages of the setup of the collection so I can follow what each method does. Here is how you print the select statement:
The above method can be found in the Zend_Db_Select class which every resource collection in Magento inherits, perhaps even every resource object inherits at some level (not 100% sure of this though). Here is a screen shot of the the Zend_Db_Select class which shows only the public methods which are usable from our level of programming. I highlighted the __toString() method for you:
Let me know how you deal with understanding the internal workings of Magento’s collections…?