1

我使用 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 中的更改。

4

1 回答 1

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)循环并避免随机键。

于 2012-04-03T08:27:38.633 回答