Magento

Magento custom collection filter and sort

Magento custom collection filter and sort

Magento custom collection filter and sort

 

We have already know how to filter magento product ,customer,wishlist,order  collection  fields filters.

But whenever we are working on  non-exiting model collection, then we facing problem.Here i have put some tricks how you can filter a custom collection by a field.In post,i have working on a writing how a custom collection is filter by a filed and how the collection will filter.

Suppose a custom collection is

#Mage::getModel('[ModulePrefix/Entities]')->getCollection();
$collection=Mage::getModel('[custommodule/customemodule]')->getCollection();

For add addFieldToSelect('*') at that Collection for retrieves all field value for a module is

Then want to filter that collection by a field name ‘age’ then use

$collection->addFieldToSelect('age');

For add for sort by a specific field then add setOrder('fieldname','SortOrder');
Example want to sort by age descending then try this

$collection->addFieldToSelect('age';'DESC');

for fetch limit number of rows of a collection use setPageSize(number_of_rows)

$collection->setPageSize(10)