Oh snap! You are using an old version of browser. Update your browser to get new awesome features. Click for more details.

Magento: Very Useful Collection Functions

There are different important functions that you can implement in your Collection object. The functions are present in Varien_Data_Collection_Db class. The class file is present in lib/Varien/Data/Collection/Db.php
Here are some of the functions that you can use in your collection object:-

 * Get Zend_Db_Select instance
 * Get collection size
 * Get sql select string or object
 * Add select order
$collection->getSelect()->setOrder($field, $direction);
 * Add field filter to collection
 * If $attribute is an array will add OR condition with following format:
 * array(
 *     array('attribute'=>'firstname', 'like'=>'test%'),
 *     array('attribute'=>'lastname', 'like'=>'test%'),
 * )
$collection->getSelect()->setOrder($field, $condition);
 * Set select distinct
 * Get all data array for collection
 * Reset loaded for collection data array
 * Print and/or log query
$collection->getSelect()->printLogQuery(true, true);
More functions below:-
Varien_Data_Collection_Db class extends Varien_Data_Collection class. Here are some more functions present in Varien_Data_Collection class:-
 * Get current collection page
 * Retrieve collection last page number
 * Retrieve collection page size
 * Retrieve collection all items count
 * Retrieve collection first item
 * Retrieve collection last item
 * Retrieve collection items
 * Clear collection
You can also use the select functions as present in Zend_Db_Select class. This class file is present in lib/Zend/Db/Select.php
Hope this helps. Thanks.

(0) comments