有没有办法用树行为对 cakePHP 中的评论进行分页?我应该使用树行为还是编写自己的代码来查看评论?
抱歉,这个问题以前有人问过。我找到了一篇文章:在 MySQL 中管理分层数据
……我将发布我的解决方案。
不,我没有找到好的解决方案。我不想重新发明轮子,我想用蛋糕的方式来做。
我写了一个帮助递归打印评论:
# view/helpers/comments
class CommentsHelper extends AppHelper{
public function printComments($comments = array(), $params = array()){
if (empty($comments) || !is_array($comments)) return false;
echo '<ul id="comments-'.$comments[0]['Forum']['id'].'">';
if (is_array($comments)){
foreach($comments as $comment):
?>
<li id="<?php echo $comment['Forum']['id']; ?>">
<div class="inner">
<h4><?php echo $comment['Forum']['title']; ?></h4>
<div class="meta">
<?php echo $comment['User']['first_name']; ?>
</div>
<div class="content">
<?php echo $comment['Forum']['content']; ?>
</div>
</div>
<?php
if (isset ($comment['children']))
if (is_array($comment['children'])){
if (!empty($comment['children'])) $this->printComments($comment['children']);
}
echo '</li>';
endforeach;
}
else{
echo '<li>'.$comment['Forum']['title'].'</li>';
}
echo '</ul>';
}
以下是我从数据库中检索数据的方法:
# controllers/forums.php
$this->set('tree', $this->Forum->find('threaded'););
问题是如何对评论进行分页?
目前我正在使用 2 个表,一个用于根目录,第二个用于线程注释。我没有找到解决办法。