cancel
Showing results for 
Search instead for 
Did you mean: 

Ordering Child Categories by id DESC

SOLVED

Ordering Child Categories by id DESC

Hi Everyone,

I have a very simple problem but I am hitting my head against the wall trying to resolve this. I am getting the list of active subcategories and I want to list them based on their id, highest to lowest so effectively the newest ones come first.

 

However regardless of which piece of code I use (below) the order remains id ASC and not id DESC. I have tried Google and stack overflow and I cannot get any of the examples to work..

Could someone please put me out of my misery and explain what it is I am doing wrong please?

Kind Regards,

Urbanwarfare

 

$subCategoryCollection = $_category->getCollection()->setStoreId(Mage::app()->getStore()->getId())->addAttributeToSelect('*')
                            ->addAttributeToFilter('parent_id', $_category->getId())
                            ->addAttributeToFilter('is_active', 1)//get only active categories if you want
                            ->addAttributeToSort('id', 'asc');

$subCategoryCollection = $_category->getCollection()->setStoreId(Mage::app()->getStore()->getId())->addAttributeToSelect('*')
                            ->addAttributeToFilter('parent_id', $_category->getId())
                            ->addAttributeToFilter('is_active', 1)//get only active categories if you want
                            ->addAttributeToSort('id', 'desc');

 

1 ACCEPTED SOLUTION

Accepted Solutions

Re: Ordering Child Categories by id DESC

Hi Ashishgk,

Your code is very similar to something I have already tried but I tried it again and here are the results I get when tweaking the field and direction on the filter.

 

1 = id, DESC

2 = position, DESC

3 = name, ASC

4 = name, DESC

 

result_examples.png

 

Now interestingly if I use id ASC - it works and I get the id's lowest to highest but it seems to resort back to an alternative filter if I use the combination of id and DESC?

 

THE SOLUTION

Well after debugging down to the controller for the addAttributeToSort function in the abstract class Mage_Eav_Model_Entity_Collection_Abstract I could see that when you call id, it appears to ignore the direction but in fact it is ignoring the request for id because id does not exist in the flat catalog table.

I had just assumed getId meant it had id in the table - but it does not this is returning the entity_id therefor if you want your code to always return the most recent at the top you need to order by entity_id DESC & use something like this:

$subCategoryCollection = $_category->getCollection()
->setStoreId(Mage::app()->getStore()->getId())->addAttributeToSelect('*')
->addAttributeToFilter('parent_id', $_category->getId())
->addAttributeToFilter('is_active', 1)//get only active categories if you want
->addAttributeToSort('entity_id', 'desc');

 

 

 

View solution in original post

2 REPLIES 2

Re: Ordering Child Categories by id DESC

<?php 
 $subcategories = Mage::getModel('catalog/category')->getCollection()
->addAttributeToSelect('name')
->addFieldToFilter('parent_id', $categoryId)
->addAttributeToSort('id', DESC);
?>

 This works if you use 'position' instead of 'id'

Re: Ordering Child Categories by id DESC

Hi Ashishgk,

Your code is very similar to something I have already tried but I tried it again and here are the results I get when tweaking the field and direction on the filter.

 

1 = id, DESC

2 = position, DESC

3 = name, ASC

4 = name, DESC

 

result_examples.png

 

Now interestingly if I use id ASC - it works and I get the id's lowest to highest but it seems to resort back to an alternative filter if I use the combination of id and DESC?

 

THE SOLUTION

Well after debugging down to the controller for the addAttributeToSort function in the abstract class Mage_Eav_Model_Entity_Collection_Abstract I could see that when you call id, it appears to ignore the direction but in fact it is ignoring the request for id because id does not exist in the flat catalog table.

I had just assumed getId meant it had id in the table - but it does not this is returning the entity_id therefor if you want your code to always return the most recent at the top you need to order by entity_id DESC & use something like this:

$subCategoryCollection = $_category->getCollection()
->setStoreId(Mage::app()->getStore()->getId())->addAttributeToSelect('*')
->addAttributeToFilter('parent_id', $_category->getId())
->addAttributeToFilter('is_active', 1)//get only active categories if you want
->addAttributeToSort('entity_id', 'desc');