ylliX - Online Advertising Network

Sorting custom collection in Magento

It seems to be trivial, but it’s not. If you want to use custom table and custom model, and sort collection by one of your table’s column, you can’t just use addAttributeToSort as you do with native Magento collection (like “catalog/product”). Instead of this, you should use setOrder, so your code should look like this:

$items = Mage::getModel('module/model')
         ->getCollection()
         ->setOrder('date_added', 'DESC')
         ->addFieldToFilter('category_id', array('eq' => $category_id))
         ->setPageSize($pageSize)
         ->setCurPage($currentPage);

 

5 thoughts on “Sorting custom collection in Magento

  1. I read a lot of interesting content here. Probably
    you spend a lot of time writing, i know how to save you a lot of time, there
    is an online tool that creates unique, google friendly articles in seconds, just search in google
    – laranitas free content source

Leave a Reply