D
D
Denis Labutin2015-04-16 13:18:23
Yii
Denis Labutin, 2015-04-16 13:18:23

How to hide a tree branch in Yii, with unlimited nesting of descendants?

Good afternoon,
there is a table in a DB where records have an unlimited nesting.
How to get a list of elements, without descendants, descendants of descendants, descendants of descendants...etc.
Getting without descendants is pretty easy
$model->findAll('parent_id !='.$id);
. But how to exclude all descendants?
Needed for editing so that the element's parent cannot be one of its children.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Denis Labutin, 2015-04-16
@lazy_den

Here is the solution
Model:

public function exceptChild($data,$rootID){
        foreach($data as $id => $node){
            if($node->parent_id == $rootID){
                unset($data[$id]);
                $data = $this->exceptChild($data,$node->id);
            }
        }
        return $data;
    }

Controller:
id - the id of the post whose branch to hide.
$data = $model->exceptChild($model->findAll('id !='.$id),$id);

S
slvABTOP, 2015-04-16
@slvABTOP

Or maybe it is worth changing the structure of the database a little?
nested sets take a look

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question