Php Codeigniter&;Datamapper:Count()注释

Php Codeigniter&;Datamapper:Count()注释,php,codeigniter,codeigniter-datamapper,Php,Codeigniter,Codeigniter Datamapper,我很难计算一篇文章有多少评论。这是一对多关系,我在comments表中有一个名为(article_id)的列,在我的索引页上显示了我想要获得每个文章的评论数的所有文章 我正在使用数据映射器。如果我有2篇文章最老的一篇有7条评论,新的一篇有2条评论,那么它将在这两篇文章上显示新文章的noOfComments。因此,不是评论:2评论:7是评论:2还是评论:2有什么想法吗 $articles = new Article_model(); $comments = new Comment_model();

我很难计算一篇文章有多少评论。这是一对多关系,我在comments表中有一个名为(article_id)的列,在我的索引页上显示了我想要获得每个文章的评论数的所有文章

我正在使用数据映射器。如果我有2篇文章最老的一篇有7条评论,新的一篇有2条评论,那么它将在这两篇文章上显示新文章的noOfComments。因此,不是评论:2评论:7评论:2还是评论:2有什么想法吗

$articles = new Article_model();
$comments = new Comment_model();

$articles->order_by('pubdate', 'desc')->get();
$id = $articles->id;

$noOfComments = $comments->where('article_id', $id)->count();

$recent_articles = array();

foreach ($articles as $article)
{
  $single_article = array(
            'id' => $article->id,
            'title' => $article->title,
            'pubdate' => $article->pubdate,
            'text' => $content,
            'cover_img' => $article->cover_img,
            'noOfComments' => $noOfComments,
            );
   array_push($recent_articles, $single_article);
 }

好的,让我们看一下代码:

// These are the datamaper objects
$articles = new Article_model();
$comments = new Comment_model();

// Get all of the articles ordered by pubdate and sorted desc
$articles->order_by('pubdate', 'desc')->get();
// $id now contains the articles
$id = $articles->id;

// Count the article id's for each comment that is associated with the article_id
$noOfComments = $comments->where('article_id', $id)->count();

// Create an array to store data
$recent_articles = array();
// Loop over the articles and create an array of information
foreach ($articles as $article)
{
  $single_article = array(
            'id' => $article->id,
            'title' => $article->title,
            'pubdate' => $article->pubdate,
            'text' => $content,
            'cover_img' => $article->cover_img,
            'noOfComments' => $noOfComments,
            );
   // push the data to $recent_articles
   array_push($recent_articles, $single_article);
 }
因此,在您看来,您使用的是$single_article和$recents_article,并且您传递的$noOfComments对于这两个数组都是相同的,这就是为什么您会得到您所拥有的输出

编辑:

$noOfComments=$comments->where('article_id',$id)->count()
我看到的只是将
文章id
传递到这个
$comments->where()函数中,该函数返回注释总数的计数。这就是它所做的一切,它没有区分新旧


问题是如何区分旧的和新的,
$comments->where()
函数看起来像什么?您能否将其扩展为接受多个id(它们的数组),以便在函数中比较它们并返回两个计数(在数组中)以添加到数组中?

假设注释与具有多对一关系的文章相关(一篇文章有多个注释):


如果出于某种原因

$related_comments = $article->comment_model->all_to_array();
不起作用,请尝试:


我只是用解析模板传递$recent_articles,并创建一个循环{articles}{/articles},以便显示它们。那么你认为这里的解决方案是什么呢?:)添加新数组元素以添加上一个注释计数。现在,您只需要从传递到查询中的文章id中获取计数,您需要解决这个问题。您可以给我一个示例,看看会是什么样子吗?您只需要执行一个查询来返回您发送到查询中的文章id的注释,因此它将获得所有注释。如何区分具有相同文章id的旧文章或新文章?你是说我需要循环并获取所有id?我想它是自动完成的?
$related_comments = $article->comment_model->all_to_array();
        $related_comments = $article->comment_model->get_iterated(); // ASSUMING YOUR TABLE IS NAMED 'comment_models'
        $nr_of_related_comments = $related_comments->result_count();

        if (count($nr_of_related_comments) > 0)
        {
            foreach ($related_comments as $related_comment) 
            {
                $comment_id = $related_comment->id;
                $recent_articles[$article_id]['related_comments'][$comment_id] = $related_comment->to_array();
            }
            unset($related_comments);
        }