Codeigniter& Datamapper:Count()注释

时间:2014-04-16 23:42:36

标签: php codeigniter codeigniter-datamapper

我无法计算文章中有多少条评论。它是一对多的关系,我在注释表中有一个名为(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);
 }

2 个答案:

答案 0 :(得分:0)

好的,让我们来看看代码:

// 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()函数是什么样的?你可以扩展它以接受多个id(它们的数组),这样你就可以在函数中比较它们并返回两个计数(在数组中)以添加到你的数组中吗?

答案 1 :(得分:0)

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

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