2014-04-16 5 views
0

기사의 댓글 수를 계산하는 데 문제가 있습니다. 일대 다 관계이고 주석 테이블에 (article_id)라는 열이 있고 내 색인 페이지에는 각 기사의 주석 수를 얻고 싶은 모든 기사가 표시됩니다.Codeigniter & Datamapper : Count() 덧글

데이터 마퍼입니다. 이 코드가 나에게주는 것 2 기사 가장 오래된 것은 7 개의 코멘트를 가지고 있고 새로운 것은 2 개의 코멘트를 가지고있다. 그러면 두 기사에 새로운 코멘트의 noOfComments가 표시 될 것이다. 2 개댓글 : 2 다시 댓글 : 2 개 어떤 아이디어 7이 댓글입니다 그래서 대신 댓글되는?

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

답변

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(); 나는 당신의 의견의 총 수의 수를 반환이 $comments->where() functionarticle id를 통과 참조 모든. 그것이 모두하고있는 일이며, 구식인지 신 구속인지 구별하지 못합니다.

구식과 신구법을 구별하는 것이 어떻게됩니까? $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(); 
이 에게

이 작동하지 않는 경우 : 댓글 가정

+0

구문 분석 템플릿과 함께 $ recent_articles를 전달하기 만하면됩니다. {articles} {/ articles} 루프를 만들어 표시 할 수있게되었습니다. 그럼 여기서 해결책이 무엇일까요? :) – Unsparing

+0

이전에 코멘트가 추가되도록 새 배열 요소를 추가하십시오. 지금 당신은 당신의 질의로 전달 된 article_id에서 카운트를 잡는 중입니다, 당신은 그것을 고칠 필요가 있습니다. – Skewled

+0

어떻게 보이게 될지 예를 들어 주시겠습니까? – Unsparing

0

는 대일 관계 기사 (한 기사에 대한 많은 댓글)과 관련이있다 시도 :

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