Useful Collection Functions of Magento



There are many different useful functions that you can implement in your Collection class. These functions are described in Varien_Data_Collection_Db class. This class file is in lib/Varien/Data/Collection/Db.php

Here i am listing some important collection functions that you can use in your collection class:

To get Zend_Db_Select instance

$collection->getSelect();

To get select string of sql object

$collection->getSelect()->getSelectSql();

To get the size of collection

$collection->getSelect()->getSize();

To get distinct selection

$collection->getSelect()->distinct($flag);

To get all data array for collection

$collection->getSelect()->getData();

To set an order to the collection

$collection->getSelect()->setOrder($field, $direction);

To print the query or log the query

$collection->getSelect()->printLogQuery(true, true);

To get collection items

$collection->getSelect()->getItems();

To get first item of the collection

$collection->getSelect()->getFirstItem();

To get last item of the collection

$collection->getSelect()->getLastItem();

To get current collection page number

$collection->getSelect()->getCurPage(); 

To get total collection pages

$collection->getSelect()->getPageSize();

To clear the collection

$collection->getSelect()->clear(); 

You can see all other collection functions in the class.