简体   繁体   English

Codeigniter和Datamapper:Count()注释

[英]Codeigniter & Datamapper: Count() comments

I am having trouble counting how many comments an article has. 我无法计算一篇文章有​​多少评论。 It is one to many relationship and I have a column in the comments table called (article_id) and on my index page where are displayed all the articles I want to get how many comments each one has. 这是一对多的关系,我在评论表中的列(article_id)上有一个列,在我的索引页上有我想要获取每个评论有多少条的所有文章。

I am using DataMapper . 我正在使用DataMapper What this code gives me if I have 2 Articles the oldest one has 7 comments and the new one has 2 comments then it will display the noOfComments of the new one on both articles. 如果我有2篇文章 ,最旧的一篇有7条评论,而新的一篇有2条评论,那么这段代码为我提供的内容将在这两篇文章中显示新一篇的noOfComments。 So Instead of being Comments:2 Comments:7 is Comments: 2 and again Comments: 2 Any Ideas ? 所以不是评论: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);
 }

Ok let's have a look over the code: 好吧,让我们看一下代码:

// 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);
 }

So in your view you are using $single_article and $recent_articles, and your passing $noOfComments which would be identical for both of the array's which is why you are getting the output you have. 因此,在您看来,您正在使用$ single_article和$ recent_articles,以及您传递的$ noOfComments对两个数组都相同,这就是为什么要获得输出的原因。

EDIT: 编辑:

$noOfComments = $comments->where('article_id', $id)->count(); All I see is your passing the article id into this $comments->where() function which is returning the count of the total number of comments. 我所看到的只是您将商品article id传递到此$comments->where() function ,该$comments->where() function返回评论总数。 That is all it's doing, it isn't distinguishing between old or new. 这就是它所做的全部,没有区分旧的还是新的。

How is it suppose to distinguish between old and new is the question, what does $comments->where() function look like? 假设如何区分新旧问题, $comments->where()函数是什么样的? Can you extend it to accept multiple id's(an array of them) so you can than compare them in the function and return both counts(in an array) to add to your array? 您是否可以将其扩展为接受多个id(它们的一个数组),因此您可以在函数中比较它们并返回两个计数(在一个数组中)以添加到数组中?

Assuming Comments is related with Articles with a many-to-one relationship (many comments for one article): 假设评论与具有多对一关系的文章相关(一篇文章的许多评论):

$articles = new Article_model(); // IS YOUR TABLE REALLY NAMED 'article_models' ?
$comments = new Comment_model(); // IS YOUR TABLE REALLY NAMED 'comment_models' ?

$articles->order_by('pubdate', 'desc')->get_iterated();
$nr_of_articles = $articles->result_count();

if (count($nr_of_articles) > 0)
{
    foreach ($articles as $article) 
    {
        $recent_articles[] = $article->to_array();

        // OR, IF LIKE ME YOU PREFER ID'S AS KEYS
        $article_id = $article->id;
        $recent_articles[$article_id] = $article->to_array();

        $related_comments = $article->comment_model->all_to_array(); // ASSUMING YOUR TABLE IS NAMED 'comment_models'
        $recent_articles[$article_id]['related_comments'] = $related_comments;
    }
    unset($articles); // REMEMBER TO FREE YOUR SERVER'S RAM! DATAMAPPER OBJECTS ARE HEAVY
}


if for some reason 如果由于某种原因

$related_comments = $article->comment_model->all_to_array();

doesn't work, try: 不起作用,请尝试:

        $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);
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM