Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Ordering model query according to it's has_many relation
  • Hi,

    Is it possible when you do a query on a model, to sort the result by the number of it's has_many's related items ? For example, order blog posts by number of comments...

    Thanks
  • HarroHarro
    Accepted Answer
    No. That would require a select with a subquery, and that is not supported.

    As a workaround you could:
    - run your query including the related has-many
    - loop over the result, and store the count of the related records in a custom variable
    - sort the array on that variable using Arr::sort()
    - send the sorted array to the view
  • Yes, I was thinking about it but the table store a lot of data so it's definilty not the good way. I'll build my requests.

    Thanks

Howdy, Stranger!

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

In this Discussion