2012-03-21 13 views
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] => 
       ) 
      ) 
     ) 
) 

のようにフォーマットされた希望私は自分自身を作成する必要がありますか?キーの変更にも注意してください。

 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)ループを行うと、ランダムなキーを避けることができたの方法:私は実際には以下のような結果から、私自身の配列を生成することになった。この問題を解決するには

答えて

関連する問題