Open ricardocanelas opened 10 years ago
you would have to recalculate rgt and lft for this. Load the tree from the DB, sort the records using PHP and save them back.
how i have to recalculate rgt and lft for this? ..or I can create a new column 'parent_id'?
Please check if I did right:
public function orderByName(){
$parent=Category::model()->findByPk(1);
$descendants=$parent->children()->findAll(array("order" => "name DESC"));
self::childrenOrderByName($descendants,$parent);
}
private function childrenOrderByName($descendants, $parent){
foreach ($descendants as $key => $category) {
$category->moveAsFirst($parent);
$_parent = Category::model()->findByPk($category->id);
$_descendants=$_parent->children()->findAll(array("order" => "name DESC"));
if(count($_descendants) > 0){
self::childrenOrderByName($_descendants,$_parent);
}
}
}
My category table
The listing is as follows:
But I wish to be like this: Sort by name:
Root
How can I do?