gabordemooij / redbean

ORM layer that creates models, config and database on the fly
https://www.redbeanphp.com
2.31k stars 280 forks source link

How to set values on a join table? (shared lists) #903

Closed fabswt closed 2 years ago

fabswt commented 2 years ago

Hey!

I have the following structure:

I can create lists (listglish) of ten items, made up of sentences (script) just fine, like so:

$scripts = R::dispense('script', 10);
foreach ($list_items as $index => $item) {
    $scripts[$index]->text = $item;
    $scripts[$index]->sharedListglishList[] = $listglish;
}
R::storeAll($scripts); // save the items and the parent list

However, I need the ability to order and re-order the items in the lists, hence the position column.

So basically I just want to set listglish_script.position to a value from 1 to 10.

What is the RedBean way of doing this please?

Thank you for the help.

gabordemooij commented 2 years ago

Use a regular bean and treat it like a sharedList using via().

https://redbeanphp.com/index.php?p=/many_to_many

I would rename listglish_script to something else, I am not familar with your application but for the sake of argument let's say we call that bean 'combination'.

Then you can use the combination bean like:

$scripts[$index]->via('combination')->sharedListglishList

You can then also use SQL extensions