Я не мог понять, как это сделать, может быть, правильно, поэтому я решил использовать элементы для достижения этой цели.По сути, на каждой итерации 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']));
}
?>
И результат: