[英]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.