RedBeanPHP

easy ORM for PHP

Shared Lists

A building can only be in one village; so in the previous example we used ownBuilding[] to store buildings. In other situations however you want to share objects. In this case use a shared list:


    $army 
R::dispense('army');
    
$village->sharedArmy[] = $army;
    
$village2->sharedArmy[] = $army;

Now both villages have the same army. Once again the name of the shared list property needs to match the type of bean it stores. In the database, RedBeanPHP will make a link table army_village to associate the armies with their villages. This is why it's called a shared list, because it's possible for one village to have the same army as another village. Contrast this to the table structure for villages and buildings as discussed in the previous chapter.

Parent objects of shared beans

What villages does an army belong to? To answer this question use:


    $myVillages 
R::related($army,'village');

For the rest, shared lists work just like own-lists.



User contributed notes. Please use the comment section to provide tips, notes and examples. To ask for support or to provide feedback use the forum. For bug reports use Github Issue Tracker.

Site comments powered by Disqus

page generated in 0.010725975036621 sec.