Magento Collection

Collection in Magento is great way to handle complex hierarchy in easy way. Collection gives us a whole load of convenience methods, making it easier to manipulate lists of entities and reducing the need to write complex sql. As Magento uses EAV model so writing Sql queries, even for simple selction, is quite difficult as it involves number of tables and joins among these tables. An example will help us to understand how to use it so let’s pick the Product entity ..

$pCollection = Mage::getModel(‘catalog/product’)->getCollection();

Now on this collection we can apply Select, Filters, Joins simply by calling methods which is the real beauty of collection

To select a single attribute: $pCollection->addAttributeToSelect(‘name’);

To select multiple attributes: $pCollection->addAttributeToSelect(‘name’, ‘url’, ‘sku’, ‘image’);

Adding filters

To get only active products: $pCollection->addAttributeToFilter(‘status’, 1);

To get products not visible individually: $pCollection->addAttributeToFilter(‘visibility’, array(“neq”=>1));

Getting products in ascending order of name:  $pCollection->addAttributeToSort(‘name’, ‘ASC’)

To know about all Collection Manipulation Methods and Filter Condition Codes please refer to this

Thank you.

Posted in:

Leave a Reply