CakePHP 3:集合:listNested

CakePHP 3:集合:listNested,cakephp,cakephp-3.0,Cakephp,Cakephp 3.0,我想用CakePHP的listNested函数展平树结构,如下所述: 我尝试了给定的示例,但没有得到示例中预期的结果。我做错了什么,或者这是listNested函数中的错误 我的代码: $data = [ [ 'id' => 1, 'parent_id' => null, 'name' => 'Birds', 'children' => [

我想用CakePHP的listNested函数展平树结构,如下所述:

我尝试了给定的示例,但没有得到示例中预期的结果。我做错了什么,或者这是listNested函数中的错误

我的代码:

$data = [
        [
            'id' => 1,
            'parent_id' => null,
            'name' => 'Birds',
            'children' => [
                ['id' => 2, 'parent_id' => 1, 'name' => 'Land Birds', 'children' => []],
                ['id' => 3, 'parent_id' => 1, 'name' => 'Eagle', 'children' => []],
                ['id' => 4, 'parent_id' => 1, 'name' => 'Seagull', 'children' => []],
            ]
        ],
        [
            'id' => 6,
            'parent_id' => null,
            'name' => 'Fish',
            'children' => [
                ['id' => 5, 'parent_id' => 6, 'name' => 'Clown Fish', 'children' => []],
            ]
        ]
    ];

$collection = new Collection($data);

$flatList = $collection->listNested();

debug($flatList->toArray());
我的结果是:

[
    (int) 0 => [
        'id' => (int) 5,
        'parent_id' => (int) 6,
        'name' => 'Clown Fish',
        'children' => []
    ],
    (int) 1 => [
        'id' => (int) 6,
        'parent_id' => null,
        'name' => 'Fish',
        'children' => [
            (int) 0 => [
                'id' => (int) 5,
                'parent_id' => (int) 6,
                'name' => 'Clown Fish',
                'children' => []
            ]
        ]
    ],
    (int) 2 => [
        'id' => (int) 4,
        'parent_id' => (int) 1,
        'name' => 'Seagull',
        'children' => []
    ]
]
预期结果:

[
    ['id' => 1, 'parent_id' => null, 'name' => 'Birds'],
    ['id' => 2, 'parent_id' => 1, 'name' => 'Land Birds'],
    ['id' => 3, 'parent_id' => 1, 'name' => 'Eagle'],
    ['id' => 4, 'parent_id' => 1, 'name' => 'Seagull'],
    ['id' => 6, 'parent_id' => null, 'name' => 'Fish'],
    ['id' => 5, 'parent_id' => 6, 'name' => 'Clown Fish']
]

看来你是对的。或者至少文件是误导性的。考虑一下开个罚单吧,看来你是对的。或者至少文件是误导性的。考虑开罚单