Search code examples
phpormredbean

How to sort/order Redbean sharedList


In my application, member entities choose a from a pre-defined set of question entities.

I save and iterate over them as a shared list ($member->sharedQuestion).

Now I need to rank them, so I add another column via the link bean (member_question) called 'position'.

My question is - can I make redbean retrieve the questions ORDERed by the column 'position'?

I currently do a

foreach($member->sharedQuestion as $question){.......}

I know I could get the array property and run it through a custom sort handler before I start iterating, but that seems expensive.

Does anyone know of a Redbean method to append some sql (i.e. "ORDER BY position") to a sharedList for example?


Solution

  • Despite having read the Redbean documentation many times, I had missed the (very simple) solution.

    Prepending the ->with() method applies extra sql to the query. So what I need to do is;

    foreach($member->with("ORDER BY position")->sharedQuestion as $question){.......}
    

    and my problem is elegantly solved!