Reputation: 3928
Using the ORM, I want to be able to load all articles that posted, in which a user has made a comment.
comments table
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.
Upvotes: 2
Views: 783
Reputation: 15192
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.
Upvotes: 5
Reputation: 10340
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
Upvotes: 0