开发者

Help with Kohana ORM?

Using the ORM, I want to be able to load all articles that posted, in which a user has made a comment.

comments ta开发者_如何学Pythonble

comment_id
user_id
article_id
etc....

Using ORM, I can access all articles posted by a user, but how would I be able to access all articles in which the user has commented on?

Thanks

Edit:

Another problem is, if a user comments on the same article twice, that article will show twice.. how do I change the group by clause of one object from within another?

Also, how do I change the order of one object from another?

Im using 2.3.4.


In pseudocode, try this...

  • In Article Model, Have $has_many = array('comments');

  • In Comment Model, Have $belongs_to = array('article');

  • And in the code where you query:

.

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

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

Sorry this won't solve duplicate article entry when $user_id comment more than once in an article... You may need to do bit more research on top of this.


ORM within kohana is mostly ment for simple tasks. While I'm sure it is possible to do this in ORM I rather use normal queries for a bit more complicated tasks:

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

something like this should do the job if I didn't make any errors

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜