2011-11-03 40 views
0

我在我的應用程序中創建了一個評論系統,它現在附加到我的articles->view。評論控制器的功能之一是能夠讓用戶回覆評論(一個級別),並能夠爲特定評論創建許多子女評論。一切都通過parent_id連接。Cakephp - 縮進相關評論

現在,當我打開的文章中,我使用foreach $comments as $comment檢索相關的文章的所有意見,並在articles_controller.php我的$comments變量分配給find相關的文章的所有意見。

什麼是最好的方式讓$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_controller.php

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/view.ctp

<?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

我有lft字段以及rght字段。但是,我的問題將是如何實現這一點。我已經做了一些更多的研究並發現了CakePHP樹行爲,但是,我還沒有圍繞如何實現它的方法來包裝我的大腦。我已經使用'foreach $ comments作爲$ comment'來顯示父註釋。我如何從那裏分支並顯示每個家長的孩子評論?我會稍後嘗試發佈一些代碼。另外,我在哪裏添加'ActsAs =數組('樹')?在「文章」或「評論」模型中?例如,從'Article'我使用'$ this-> Article-> Comment-> id'。 –

0

我找到了一個解決方案,不要使用RequestAction作爲縮進註釋。基本上,在視圖中,我顯示的評論沒有回覆,有一個元素。我通過這個元素傳遞整個comments數組。在這個陣列中,如果我們有我們顯示評論匹配「父ID」的註釋,有可能再次循環,等等一個foreach ...

在郵政/ view.ctp:

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'] 

In Element/comment。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)); 
    } 
}