I have a cakephp app with a category model that acts as a Tree. I imported ~100 categories through a csv file.
I want to be able to quickly reorder all the categories by name so that my index view is ordered correctly.
I tried the reorder method from the Tree behavior :
$this->Category->reorder(array(
'id' => null,
'field' => 'Category.name',
'order' => 'ASC'
));
But it doesn’t do anything. Am I missing something here ?
Thanks 🙂
I have just been looking into this and have come to the conclusion it is not possible to order the results in the
generatetreelist()method – at least no easy way without looping through and manually moving each node. The best way to go about this is use the normal find method with the first argument as ‘threaded’, then you can apply SQL ordering like you would any query. I have this method in my Category model:This returns me an array of all my parent categories alphabetically-ordered, with a ‘children’ key containing another array all the children of that category, in alphabetical order too! I use
contain => falseto cut down on the data returned (threaded returns data about each node’s parent, which we don’t need to know), you could alter it to include extra data if need be.I understand this isn’t 100% the same as
generatetreelist(), but it’s certainly tidier and a lot more flexible as you control the output. If you really needed to emulategeneratetreelist()‘s behavior, you could just loop through the array from above like so:Hope this helps.