2011-11-03 4 views
0

내 앱에 내 articles->view에 첨부 된 주석 시스템을 만들었습니다. 주석 컨트롤러의 기능 중 하나는 사용자가 주석 (한 수준)에 응답하고 특정 주석에 대해 많은 하위 주석을 작성할 수 있도록 허용하는 것입니다. 모든 것이 parent_id를 통해 연결됩니다. 내가 기사를 열 때Cakephp - Indented Related Comments

는 지금, 나는 기사와 관련된 모든 의견을 검색 할 foreach $comments as $comment를 사용하고 articles_controller.php에 나는 find에 기사와 관련된 모든 의견을 $comments 변수를 지정합니다.

아이들의 의견을 $comment으로 가져와 내보기에 표시하는 가장 좋은 방법은 무엇입니까?

내가 접근하는 방식은 $comments->id을 기반으로 한 어린이 댓글 목록을 반환하고 아래 그림에 $comment 아래에 표시하는 기능을 갖는 것입니다. 이것은 전화가 이뤄지면서 시간이 많이 소요되는 것 같습니다. 함수 (즉, childrenComments($id))를 각 주석에 추가 할 수 있으며 서버를 완전히 중단시킬 수 있습니다.

누구나 parent_id 관계를 사용하고 CPU 시간을 절약하는 더 좋은 방법을 알고 있습니까?

Example - "Children" comment is manually added at this time

는 당신의 도움에 대해 감사합니다.

답변

0

이 작업을 수행하는 방법을 알아 내지 못했습니다. 올바른 방법 일 수 있으므로 요소를 사용하여이를 수행하기로했습니다. 기본적으로 foreach $comments의 반복마다 childrenComments 요소를 호출하여 현재 $comment['Comment']['id']에 대한 아동 코멘트가 있는지 확인합니다. 댓글이 있으면 각 댓글을 부모 댓글 아래에 각각 표시합니다. 내 /articles/view.ctp에

function childrenComments($id = null){ 
    $comments = $this->Article->Comment->find(
     'all', 
     array(
      'fields' => array(
       'Comment.id', 
       'Comment.comment', 
       'User.username', 
       'User.image' 
      ), 
      'conditions' => array(
       'Comment.parent_id =' => $id, 
       'Comment.approved =' => 1 
      ) 
     ) 
    ); 
    return $comments; 
} 

요소 childrenComments.ctp

<div style="width:100%;"> 
<div> 
    <?php 
    $childrenComments = $this->requestAction('/articles/childrenComments/'.$id); 
    foreach ($childrenComments as $childrenComment){ 
    ?> 
    <table cellpadding=0 cellspacing=0> 
     <tr> 
      <td> 
       <img style="width:30px;margin-right:5px" src="<?php echo $childrenComment['User']['image'];?>"> 
      </td> 
      <td width=100% style="padding-left:10px;"> 
       <?php 
        echo $childrenComment['Comment']['comment']; 
       ?> 
      </td> 
     </tr> 
    </table> 
    <?php 
     } 
    ?> 
</div> 
에게 articles_controller.php에 ....

코드를

코드를 아래의 일부 코드를 참조

<?php 
    foreach($comments as $comment){ 
... 
//Code are placed here to display each parent comment 
    ... 

     //This will iterate and display all children comments 
     echo $this->element('childrenComments',array('id' => $comment['Comment']['id'])); 
    } 
?> 

그리고 결과는 다음과 같습니다. Actual result

0

중첩 된 집합 트리가 "왼쪽"필드로 정렬되는 경우. 만약 당신이 parent_id를 가지고 있다면 나는 창조 날짜에 의해 그것들을 주문해야한다고 생각하고 그들이 속한 것에 상관없이 외래 키와 관련된 모든 레코드를 가져와야한다고 생각합니다.

+0

나는 필드뿐만 아니라 필드도 가지고 있습니다. 그러나, 내 문제는 어떻게 그것을 달성하는 것입니다. 좀 더 연구를 해보았지만 CakePHP Tree Behavior를 발견했지만, 그것을 달성하는 방법에 대해 두뇌를 감쌌습니다. 나는 이미 부모 코멘트를 표시하기 위해'foreach $ comments as $ comment'를 사용합니다. 어떻게 그곳에서 출발하여 각 부모의 자녀들의 의견을 표시 할 수 있습니까? 나는 나중에 약간의 코드를 게시하려고 노력할 것이다. 또한'ActsAs = array ('Tree')'를 어디에 추가합니까? 'Article' 또는'Comment' 모델에서? 'Article'에서'$ this-> Article-> Comment-> id'를 사용합니다. –

0

들여 쓰기 설명을 위해 RequestAction을 사용하지 않는 해결책을 발견했습니다. 기본적으로보기에서 나는 엘리먼트없이 응답없이 코멘트를 표시한다. 이 요소를 통해 전체 주석 배열을 전달합니다.

foreach ($post['Comment'] as $comment) { 
    echo $this->element('comment', array('comments'=> $post['Comment'], 'id'=> $comment['id'], 'comment'=> $comment, 'user'=> $comment['User'], 'postId'=> $post['Post']['id'], 'level'=> '1')); 
} 

// Notice the whole comments array passed in the element 'comments'=> $post['Comment'] 
: 우리는 우리가 표시하는 의견 일치 "부모 ID"에 코멘트가있는 경우이 배열에서, 그래서 다시 루프, 그리고 foreach는 ... 포스트/view.ctp에서

이있다

요소/설명.ctp :

// After the code that displays comment content 

foreach ($comments as $comm) { 
    if ($comm['parent_id'] == $comment['id']) { 
    echo $this->element('comment', array('comments'=> $comments, 'id'=> $comm['id'], 'comment'=> $comm, 'user'=> $comm['User'], 'postId'=> $postId, 'level'=> $level)); 
    } 
}