2010-01-29 18 views
2

使用ORM,我希望能夠加載發佈的所有文章,其中用戶發表了評論。幫助Kohana ORM?

意見表

comment_id 
user_id 
article_id 
etc.... 

使用ORM,我可以訪問由用戶發佈的所有文章,但如何將能夠訪問用戶已評論的所有文章?

感謝

編輯:

的另一個問題是,如果兩次在同一篇文章,用戶評論,這篇文章會告訴兩次..我如何從一個對象的條款更改組在另一個?

另外,如何更改另一個對象的順序?

使用2.3.4的Im。

+0

您使用的是哪個版本的Kohana? – dusan 2010-01-29 17:56:59

回答

0

kohana內的ORM主要用於簡單任務。雖然我敢肯定,這是可以做到這一點OR​​M我寧願用正常的查詢,對於稍微複雜的任務:

$this->db = Database::instance(); 
$this->db->query(" 
    select 
     user_id 
     article_id 
     id as comment_id 
    from 
     comments 
    left join 
     articles 
    on 
     articles.id = comments.article_id 
    where 
     user_id = $user_id 
    group by 
     comments.article_id 
"); 

這樣的事情應該做的工作,如果我沒有做任何錯誤

5

僞代碼,試試這個...

  • 在文章的模式,紛紛$has_many = array('comments');

  • 在評論模型,有$belongs_to = array('article');

  • 而且在您查詢代碼:

$comments = ORM::factory('comment') 
       ->where('user_id', $user_id); 
       ->find_all(); 

foreach($comments as $comment) 
{ 
    $article_id = $comment->article->id; 
} 

對不起這解決不了的時候$user_id的評論,而在一篇文章中曾詳細...你可能需要做更多的研究一點在此之上重複的文章條目。

+0

也許它不像我認爲的那樣是僞代碼...... = – 2010-02-01 02:13:14