Lithium - 根据关系查找树

时间:2012-03-21 16:08:26

标签: php lithium

我使用包含注释的Lithium创建了一个RESTful PHP Web服务,每个注释都可以有一个父注释,允许注释无限递归。

我使用正确的密钥在我的模型中建立了关系。

我的数据目前已格式化列表(使用Model::()):

Array
(
    [1C19FA9D-0432-A382-5236-2C59E0967F58] => Array
        (
            [id] => 1C19FA9D-0432-A382-5236-2C59E0967F58
            [page_id] => 0384F94C-8B99-F692-62D0-7B24B0885257
            [parent_id] => 
            [user_id] => 4
            [comment] => This is a test
            [created] => 2012-03-16 16:41:33
            [updated] => 
        )

    [3B2350BA-9BA7-D7D4-2ED4-42BD40BC1AF0] => Array
        (
            [id] => 3B2350BA-9BA7-D7D4-2ED4-42BD40BC1AF0
            [page_id] => 0384F94C-8B99-F692-62D0-7B24B0885257
            [parent_id] => 1C19FA9D-0432-A382-5236-2C59E0967F58
            [user_id] => 543
            [comment] => Testing
            [created] => 2012-03-16 17:25:47
            [updated] => 
        )

    [4CFD2D8B-D05F-7C8A-E2A9-38D5677280A9] => Array
        (
            [id] => 4CFD2D8B-D05F-7C8A-E2A9-38D5677280A9
            [page_id] => 0384F94C-8B99-F692-62D0-7B24B0885257
            [parent_id] => 1C19FA9D-0432-A382-5236-2C59E0967F58
            [user_id] => 53
            [comment] => A Test
            [created] => 2012-03-16 17:25:38
            [updated] => 
        )
)

我希望它的格式是这样的

Array
(
    [0] => Array
        (
            [id] => 1C19FA9D-0432-A382-5236-2C59E0967F58
            [page_id] => 0384F94C-8B99-F692-62D0-7B24B0885257
            [parent_id] => 
            [user_id] => 4
            [comment] => This is a test
            [created] => 2012-03-16 16:41:33
            [updated] =>
            [comment] => Array(
                [0] => Array
                (
                    [id] => 3B2350BA-9BA7-D7D4-2ED4-42BD40BC1AF0
                    [page_id] => 0384F94C-8B99-F692-62D0-7B24B0885257
                    [parent_id] => 1C19FA9D-0432-A382-5236-2C59E0967F58
                    [user_id] => 543
                    [comment] => Testing
                    [created] => 2012-03-16 17:25:47
                    [updated] => 
                )
                [1] => Array
                (
                    [id] => 3B2350BA-9BA7-D7D4-2ED4-42BD40BC1AF0
                    [page_id] => 0384F94C-8B99-F692-62D0-7B24B0885257
                    [parent_id] => 1C19FA9D-0432-A382-5236-2C59E0967F58
                    [user_id] => 543
                    [comment] => A Test
                    [created] => 2012-03-16 17:25:47
                    [updated] => 
                )
            )
        )
)

是否有一个内置于Lithium的递归函数,或者这是我必须自己创建的东西?另请注意Keys中的更改。

1 个答案:

答案 0 :(得分:0)

为了解决这个问题,我实际上最终从结果中生成了我自己的数组,如下所示:

       public function index($page_id) {
            $page = Pages::first($page_id);
            $site = Sites::first($page->site_id);
            $comments = Comments::all(array('conditions' => array('page_id' => $page->id, 'comment_id' => NULL)));

            if ($this->request->type == 'JSON')
                $comments = $this->_recursiveComments($comments, $page);

            return compact('comments', 'page', 'site');
        }

        protected function _recursiveComments($comments, $page) {
            foreach($comments as $c) {
                $children = Comments::all(array('conditions' => array('comment_id' => $c->id)));
                $c->children = $children;

                $this->_recursiveComments($children, $page);
            }

            return $comments;
        }

这样我就可以进行foreach($comments as $key => $value)循环并避免使用随机密钥。