Book Image

Magento PHP Developer????s Guide, 2nd Edition

By : Allan MacGregor
Book Image

Magento PHP Developer????s Guide, 2nd Edition

By: Allan MacGregor

Overview of this book

Table of Contents (16 chapters)
Magento PHP Developer's Guide Second Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Working with Magento collections


If you look back at the previous code example, you will notice that I'm not only instantiating a product model, but I'm also calling the getCollection() method. The getCollection() method is part of the Mage_Core_Model_Abstract class, meaning, every single model inside Magento can call this method.

Tip

All collections inherit from Varien_Data_Collection.

A Magento collection is basically a model that contains other models. So instead of using an array to hold a collection of products, we will use a product collection. Collections not only provide a convenient data structure to group models, they also provide special methods that we can use to manipulate and work with a collection of entities.

Some of the most useful collection methods are:

  • addAttributeToSelect: To add an attribute to the entities in a collection; * can be used as a wildcard to add all the available attributes

  • addFieldToFilter: To add an attribute filter to a collection; this function is used...