CakePHP - 打印帖子的孩子,如何

时间:2011-08-08 01:11:24

标签: cakephp foreach parent children

以下是posts_controller的调试输出。我目前使用语法:

<?php foreach ($posts as $post): ?> ... <?php endforeach; ?>

打印视图中的帖子,但是这只会产生最外面的帖子(即 - 父帖子和打印的子帖子。)

问题:

如何在每个父帖子下直接打印所有子帖子? 该解决方案是嵌入式foreach循环吗?

DEBUG:

Array
(
    [0] => Array
        (
            [Post] => Array
                (
                [active] => 1
                [id] => 1
                [parent_id] => 0
                [created] => 2011-08-06 03:54:07
                [modified] => 2011-08-06 03:54:07
                [text] => a
            )

        [Children] => Array
            (
                [0] => Array
                    (
                        [active] => 1
                        [id] => 3
                        [parent_id] => 1
                        [created] => 2011-08-08 01:54:24
                        [modified] => 2011-08-08 01:54:24
                        [text] => c
                    )

                [1] => Array
                    (
                        [active] => 1
                        [id] => 2
                        [parent_id] => 1
                        [created] => 2011-08-06 03:54:37
                        [modified] => 2011-08-06 03:54:37
                        [text] => b
                    )

            )

    )

)

2 个答案:

答案 0 :(得分:1)

$childPosts = Set::extract('/Children', $posts);

答案 1 :(得分:1)

Is it an embedded foreach loop the solution?呃..是的。我不认为还有另一种方式。

foreach ($posts as $post):
    echo $post['Post']['id'];
    foreach ($post['Children'] as $child_post){}
  endforeach;