- Getting started
- Basics
- Finding
- Relation Mapping
- One-to-many
- Many-to-many
- Parent Object
- Aliasing
- Trees
- Models
- Database
- BeanCan
- Advanced
- Architecture
- Other
Adding Lists
A bean can contain a list of other beans. A list is a property that contains an array with 0 or more beans. There are 2 kinds of lists, the own-list and the shared-list. To add a list to a bean add a property ownX or sharedX where X is the type of beans that are meant to be in the list. Once stored, lists will be retrieved the moment you access the property (we call this lazy loading). To retrieve all the pages of a book:
$pages = $book->ownPage; //returns all pages that belong to this book
The own-list contains beans exclusively owned by that bean, the shared-list contains beans that can be owned by multiple beans of the same type. Let's focus on the own-list. If you put a bean in one own-list and then move it to another list of a bean with the same type, the other bean steals the bean from the former. Now let's see how to use own-lists. A village contains buildings; a building can only be in one village at a time:
$village = R::dispense('village');
list($mill,$tavern) = R::dispense('building',2);
$village->ownBuilding = array($mill,$tavern); //replaces entire list
R::store($village);
This example creates two buildings; a mill and a tavern and puts them in the ownBuilding list of the village. They now belong to the village. If there were any buildings already in this list they have now been overwritten because of the assignment. Use:
$village->ownBuilding[] = $building; //add a building
to add them instead of replacing the entire list. Storing the village will also store all the added beans (if needed) and store the relationships. If you store the village and reload it, you will find your buildings in the property ownBuilding.
$myBuildings = $village->ownBuilding; //get buildings in village
Note that the name of the property has to match the type of beans you are storing in it, ownBuilding can only contain buildings, this is why the list is called ownBuilding. If you store pages, use ownPage, if you store cars, use ownCar etc... In the database, the buildings will be stored in a table called 'building', because that is the type of the bean. Each building will have a foreign key to the village named village_id that references the appropriate record in the village table.
To add a building later on, without removing older buildings:
$house = R::dispense('building');
$village->ownBuilding[] = $house;
To delete a building from the list use unset()...
unset($village->ownBuilding[2]); //2 is the id of the building, keys are ids
R::store($village);
Of course you can also change buildings in the list:
$village->ownBuilding[1]->name = 'The Old Inn';
To access a bean in a list, use the ID of the bean as a key,
i.e. $village->ownBuilding[ $building_id ] = ...
By default if you remove a bean from a list the connection will be broken but the bean will remain in the database. If you want RedBeanPHP to remove beans for you that have been removed from lists use:
R::dependencies(array('page'=>array('book','magazine')));
unset($book->ownPage[$id]); //-- will now also destroy the page.
From RedBeanPHP 3.2 on making beans dependent on other beans will also add an ON CASCADE DELETE trigger on the corresponding foreign key.
The best way to familiarize yourself with lists is to experiment a little.
Tweet
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