Mount tree of objects

1

I need to mount a tree of php objects, where I have for example:

id | parent_id | nome | telefone | email

from the last id I search and mount the whole tree of 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 this: but I could not 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;
} 
    
asked by anonymous 03.08.2018 / 14:02

0 answers