0

I have written a module to add my own events, and in the home page i would like to show only latest events. So in that case i have to add order by to query but i couldnot do that, it always throws fatal error.

This is what i have done.

$_offers = Mage::getSingleton('offerings/offerings')->getCollection();  

This returns all the records, here i could able to set filter options but i could not add sort order like this

$_offers = Mage::getModel('offerings/offerings')->getCollection()
            ->addAttributeToSort('offerings_id', 'DESC')
            ->setPageSize(5)
            ->setPage(1, 5);

or even using Mage::getSingleton. Whats the problem here am facing. Please help me

Elamurugan
  • 3,204
  • 13
  • 59
  • 104
  • 1
    What class does your offerings collection extend? Is it EAV based? Also, what is the fatal error thrown? – clockworkgeek Oct 27 '10 at 16:41
  • it doesnot extend EAV Class and so i could nto use the sort filter method. it throws Fatal Error : addAttributeToSort method couldnot find... – Elamurugan Oct 27 '10 at 20:48

2 Answers2

2

I haven't put in the time to test but I suspect you need to do something like this:

$_offers = Mage::getModel('offerings/offerings')->getCollection()
            ->setOrder('offerings_id', 'DESC')
            ->setPageSize(5);

As pointed out the EAV method addAttributeToSort() won't work here. Nor will setPage() but setPageSize() is just as good.

There are plenty of tutorials and guides around to learn this stuff from. Alan's knowledgebase articles are the authoritative resource on the subject, you would do well to read and practice it all.

clockworkgeek
  • 37,650
  • 9
  • 89
  • 127
0

Include an error message and people can start helping you.

My guess if you've used Module Creator to create your module, which gives you a default non-eav model and collection. The addAttributeToSort method only exists for EAV collections.

There's more information on your various sorting options here

Magento get a product collection in an arbitrary order

Community
  • 1
  • 1
Alana Storm
  • 164,128
  • 91
  • 395
  • 599
  • exactly, you are right. Used Module creator and it doesnot extend the EAV classes, so how could i use the query to sort it out. thanks – Elamurugan Oct 27 '10 at 20:46