在cakephp中创建新闻源

时间:2013-12-03 15:14:32

标签: cakephp

我在cakephp中创建了一个新闻提要功能,几乎就像facebook新闻提要一样。 发布帖子的主题和回复评论。帖子的回复不正确。单个回复重复所有帖子,循环或获取数据有一些问题。回复应出现在每个主题下面,如下所示。

Topic1
  reply1fortopic1
  reply2fortopic1
Topic2
  reply1topic2
  reply2topic2

这是我的代码

我正在使用两个表newsfeed,newsreplies。模型中提到了关系

控制器:

$newsfeed=array('group_id'=>$groupdata['Group']['id'],'status'=>'A');
$this->set('newsfeed',$this->paginate('NewsFeed',$newsfeed));
$newsdata=$this->paginate('NewsFeed',$newsfeed);


$newsreplies=$this->NewsFeed->find('all', array('conditions'=>array('NewsFeed.group_id'=>$group,'NewsFeed.status'=>'A')));
$this->set('newsreplies',$newsreplies);

查看:

<?php foreach ($newsdata as $newsdatas): ?>

           <div class="news_reply_display">
                    <?php echo $newsdatas['NewsFeed']['title']; ?>


            <?php foreach ($newsreplies as $newsreplly): ?>
                       <div class="news_reply_display">

                         <?php echo $newsreplly['NewsFeedComment']['comments']; ?>

                      </div>
            <?php endforeach; ?>
          </div>
                <?php endforeach; ?>

型号:

NewsFeed.php

<?php
class NewsFeed extends AppModel {
var $name = 'NewsFeed';
var $useTable = 'news_feeds';
var $primaryKey = 'id';
var $hasMany = array(

'NewsFeedComment' => array(
'className' => 'NewsFeedComment',));
var $belongsTo = array(
'Group'=> array ('className' => 'Group','foreignKey' => 'group_id'),
'User'=> array ('className' => 'User','foreignKey' => 'user_id'));
}
?>

NewsFeedReply.php

<?php
class NewsFeedReply extends AppModel 
{
var $name = 'NewsFeedReply' ;
var $useTable = 'newsreplies';
var $primaryKey = 'id';
var $belongsTo = array(
'NewsFeed'=> array ('className' => 'NewsFeed','foreignKey' => 'news_feed_id'),
'User'=> array ('className' => 'User','foreignKey' => 'user_id'));
}

&GT;

0 个答案:

没有答案