yiiext / nested-set-behavior

AR models behavior that allows to work with nested sets tree.
http://www.yiiframework.com/extension/nestedsetbehavior/
BSD 3-Clause "New" or "Revised" License
157 stars 64 forks source link

How can I sort by name? #36

Open ricardocanelas opened 10 years ago

ricardocanelas commented 10 years ago

My category table captura de tela 2014-05-30 as 16 29 42

The listing is as follows: captura de tela 2014-05-30 as 16 29 49

But I wish to be like this: Sort by name:

Root

How can I do?

cebe commented 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.

ricardocanelas commented 10 years ago

how i have to recalculate rgt and lft for this? ..or I can create a new column 'parent_id'?

ricardocanelas commented 10 years ago

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);
            }
        }
}