繁体   English   中英

Codeigniter和Datamapper:Count()注释

[英]Codeigniter & Datamapper: Count() comments

我无法计算一篇文章有​​多少评论。 这是一对多的关系,我在评论表中的列(article_id)上有一个列,在我的索引页上有我想要获取每个评论有多少条的所有文章。

我正在使用DataMapper 如果我有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和$ recent_articles,以及您传递的$ noOfComments对两个数组都相同,这就是为什么要获得输出的原因。

编辑:

$noOfComments = $comments->where('article_id', $id)->count(); 我所看到的只是您将商品article id传递到此$comments->where() function ,该$comments->where() function返回评论总数。 这就是它所做的全部,没有区分旧的还是新的。

假设如何区分新旧问题, $comments->where()函数是什么样的? 您是否可以将其扩展为接受多个id(它们的一个数组),因此您可以在函数中比较它们并返回两个计数(在一个数组中)以添加到数组中?

假设评论与具有多对一关系的文章相关(一篇文章的许多评论):

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


如果由于某种原因

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

暂无
暂无

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

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