How do you filter in many to many relationship?
Closed this issue · 1 comments
Following the example on the guide page, how would you retrieve posts that belongs to one or more specific category?
user: id, name
post: id, title, body, date_published, is_published, user_id
categorization: category_id, post_id
category: id, title
Is this the best way to go?
foreach( $db->categorization()
->where( 'category_id', array('14', '15', '18') )
->paged( 10, 1 )
->post() as $post ) {
// do what you want with the data here
}
Any way I can order that by post.date_published
?
Essentially what I want is 10 of the newest posts in one or more categories. Ordering by categorization.id DESC
is not a reliable method as date_published
is subject to changes.
Edit:
Well that was a rather stupid thing to ask, new to ORM :(
This is how to do it:
foreach( $db->categorization()
->where( 'category_id', array('14', '15', '18') )
->orderBy('id', 'DESC')
->paged( 10, 1 )
->post()
->orderBy('date_published', 'DESC') as $post ) {
// do what you want with the data here
}
Don't think this is the right way of going about it. If a post has a recent timestamp in date_published
but was added a long time ago (very old id
) it would not show up in the query.
OrderBy() was used twice.
Just removing the ordering by ID should fix the issue about not showing very old IDs.