Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
IN clause on a related table
  • Hi (again), I have a Model_Blog with a many_many relationship to Model_Category. So blog_id=1 is related to category_id (2,3)
    and blog_id=4 is related to category_id (2,3) as well
    and some other blog related to other categories (this doesn't matter) My problem is below :
    $query = Model_Blog::find()->related('categories')->where(array('categories.id', 'in', array(2,3));
    
    // Returns 2, as expected
    $query->count();
    
    // Returns only the first blog post, because it belongs to 2 categories and the limit applies to them, which is frustrating
    $query->rows_offset(0)->rows_limit(2);
    

    Basically I would like to be able to group by blog_id (= primary key), so many categories only result in one row in the result set. That's what the count() does automatically internally. So how should I paginate my results? (how to determine the appropriate values to insert in rows_offset() and rows_limit() )?
  • This is why the non "rows_" prefixed offset/limit methods force a subquery. But the truth is some things just can't be done in 1 query or need stronger methods (I have some Database Views setup with additional columns that count the related table's rows in common used conditions).
  • I don't need to selected the categories, I just need them to filter the blog posts. And I want the limit applied to the posts, not the categories. I think just a group_by() method would help a lot to accomplish this in 2 queries (first one to retrieve all the paginated IDs, second one to get the actual rows).
  • I'm having this issue too. Leads to odd results in pagination as some pages are missing items. Was a solution ever found?
  • If you're only interested in your primary model data, why not run a standard DB query, and use as_object() to have it return model instances?

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion