- Getting started
- Basics
- Finding
- Relation Mapping
- One-to-many
- Many-to-many
- Parent Object
- Aliasing
- Trees
- Models
- Database
- BeanCan
- Advanced
- Architecture
- Other
Parent Object
You have seen in the previous chapters how to add a building to a village. But how about getting the village a building belongs to? To add buildings to a village we used something like $village->ownBuilding[] = $building. If we now start from the building and we want to obtain a reference to the village this building belongs to:
$village = $building->village;
You can also change the parent object if you like however I consider this bad practice. To break the relation with the parent object use:
unset($building->village); //works, neat everyone knows what you mean
$building->village = null; //works, bit messy
$building->village = false; //ugly
Trying to assign a something other than a bean to a parent object field will throw an exception:
$building->village = 'Lutjebroek'; //throws a RedBeanPHP Security exception
This means that once a property has been used to store a bean, it can only be used to store a bean afterwards.
Note that the name of the property needs to match the type of bean it stores.
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