Mount object tree

Asked

Viewed 59 times

1

I need to Mount a php object tree, where I have for example:

id | parent_id | nome | telefone | email

from the past id I search and mount all the tree from that idPai

(int) 0 => object(App\Model\Entity\User) {

    'id' => (int) 192,
    'parent_id' => (int) 120,
    'nome' => 'Rubens ',
    'telefone' => '',
    'email' => '@hotmail.com',

    'children' => [
        (int) 0 => object(App\Model\Entity\User) {

            'id' => (int) 712,
            'parent_id' => (int) 192,
            'nome' => 'Sandro',
            'telefone' => '(62)',
            'email' => '@gmail.com'
        },
        (int) 1 => object(App\Model\Entity\User) {

            'id' => (int) 988,
            'parent_id' => (int) 192,
            'nome' => 'marcelo',
            'telefone' => '(84)',
            'email' => '@gmail.com',
            'children' => [
                (int) 0 => object(App\Model\Entity\User) {

                    'id' => (int) 10263,
                    'parent_id' => (int) 988,
                    'nome' => 'Rogaciano',
                    'telefone' => '(84)',
                    'email' => '@gmai.com'
                },
                (int) 1 => object(App\Model\Entity\User) {

                    'id' => (int) 10998,
                    'parent_id' => (int) 988,
                    'nome' => 'Rivanaldo',
                    'telefone' => '(84)',
                    'email' => '@gmail.com'


                },
                (int) 2 => object(App\Model\Entity\User) {

                    'id' => (int) 11113,
                    'parent_id' => (int) 988,
                    'nome' => 'Valdomiro',
                    'telefone' => '(84)',
                    'email' => '@gmail.com'
        },
        (int) 2 => object(App\Model\Entity\User) {

            'id' => (int) 3166,
            'parent_id' => (int) 192,
            'nome' => 'robson',
            'telefone' => '',
            'email' => '@gmail.com'
        },
        (int) 3 => object(App\Model\Entity\User) {

            'id' => (int) 3490,
            'parent_id' => (int) 192,
            'nome' => 'jairo',
            'telefone' => '',
            'email' => '@gmail.com',

        },
        (int) 4 => object(App\Model\Entity\User) {

            'id' => (int) 4368,
            'parent_id' => (int) 192,
            'nome' => 'anderson ',
            'telefone' => '',
            'email' => '@gmail.com',
            'children' => [
                (int) 0 => object(App\Model\Entity\User) {

                    'id' => (int) 58235,
                    'parent_id' => (int) 4368,
                    'nome' => 'Milene ',
                    'telefone' => '(16) ',
                    'email' => ''
                }
            ]
        },
}

I tried it like this: but I couldn’t get the object, just mount a tree with the data:

public function getArvore($revenda_id, $level){
    // children
    $arvore['children'][] = "";
    $users = $this->find()
        ->where(['parent_id' => $revenda_id]);

    foreach($users as $key => $user){
        echo str_repeat('  ',$level).$user->username."\n"; 
        $this->getArvore($user->id, $level+1);
    }

    return $arvore;
} 
  • And what did you try to do? What was the result?

  • I updated the question. @Andersoncarloswoss

  • If you are using Doctrine ORM, I recommend the extension https://github.com/Atlantic18/DoctrineExtensions/blob/v2.4.x/doc/tree.md which has this purpose of assembling hierarchical structures.

  • I’m not. @Humbertorodrigues

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.