2012-08-02 109 views
0

我有点新cakephp,我想知道为什么我有这种问题。cakephp螺纹评论

基本上,我在cakephp中做了一个线程注释。但我的问题是,每次我尝试对“评论”进行“评论”时,它的显示方式都不相同。

下面是截图: enter image description here

我希望它被逆转像所有孩子意见应在最后一行上予以公布。目前,当我添加新评论时,它显示在顶部而不是底部。我希望它变得像Facebook如何评论。

这里是我的这段代码:

$comments = $this->UserDiscussionComment->find('threaded', array('conditions' => array('UserDiscussionComment.user_discussion_id' => $slug), 'order' => array('UserDiscussionComment.id DESC', 'UserDiscussionComment.created ASC'))); 

这里是在数据库中的样本记录:

enter image description here

我想改变的孩子注释的顺序。我尝试过“ASC”和“DESC”,但它不起作用。

在此先感谢。

+0

array('UserDiscussionComment.id DESC','UserDiscussionComment.created ASC') – 2012-08-02 08:27:55

+0

顺便说一句,多线程标签不适合这个问题。 – 2012-08-02 09:18:55

回答

2

你将不能够根据对find('threaded')文档中不同的顺序命令孩子们。我会做的是你发现电话后,简单地恢复数组:

$comments = $this->UserDiscussionComment->find('threaded', array(
    'conditions' => array('UserDiscussionComment.user_discussion_id' => $slug), 
    'order' => array('UserDiscussionComment.id DESC') 
)); 

for ($i = 0; $i < sizeof($comments); $i++) { 
    $comments[$i]['children'] = array_reverse($comments[$i]['children']); 
} 

未经测试,但它应该做的伎俩(我也想你只能评1级低沉,像你的屏幕截图所示)。


编辑

我想和大家分享我过去使用您要完成同样的事情,不同的方法。基本上,我把我的Comment模型像这样:

class Comment extends AppModel { 

    public $belongsTo = array(
     'ParentComment' => array(
      'className' => 'Comment', 
      'foreignKey' => 'parent_id' 
     ), 
     'User' 
    ); 

    public $hasMany = array(
     'ChildComment' => array(
      'className' => 'Comment', 
      'foreignKey' => 'parent_id' 
     ) 
    ); 
} 

然后,当我想做一个查找,我可以订购不同父&孩子的意见(请注意,我用的Containable行为):

$comments = $this->Comment->find('all', array(
    'order' => 'Comment.id DESC', 
    'contain' => array(
     'ChildComment' => array(
      'order' => 'ChildComment.id ASC' 
     ) 
    ) 
)); 
+0

hi Hoff!你的解决方案非常棒!我现在能够对所有东西进行排序。谢谢谢谢谢谢! – comebal 2012-08-02 23:51:12

+0

@comebal hello,如果有像用户那样的n个回复评论和用户B回复它,并且用户C回复这样的用户B回复。你能给我任何解决方案或建议吗? – Tarzan 2017-12-11 13:40:30

0
array('UserDiscussionComment.id DESC', 'UserDiscussionComment.created ASC') 

我认为错误在那里。

由于UserDiscussionComment.id全部不同,所以没有任何东西可以订购UserDiscussionComment.created ASC

尝试:

array('UserDiscussionComment.parent_id DESC', 'UserDiscussionComment.created ASC') 
+0

嗨,谢谢你的回应。但是,我应用了你的建议后,它仍然显示我错误的结果:( – comebal 2012-08-02 09:42:48

+0

什么是数组('UserDiscussionComment.parent_id DESC','UserDiscussionComment.created DESC')呢? – 2012-08-02 10:23:13

+0

它正确显示子注释,但主要注释是倒。父母的评论正在显示从最旧到最新,这应该是另一种方式......我真的需要你的帮助。在此先感谢 – comebal 2012-08-02 11:50:54