Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sort/order Redbean sharedList

Tags:

php

orm

redbean

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?

like image 764
charliefortune Avatar asked Sep 02 '25 03:09

charliefortune


1 Answers

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!

like image 182
charliefortune Avatar answered Sep 05 '25 00:09

charliefortune