- General
- Articles
- Legacy
- Misc
- Third Party
- Who uses RedBeanPHP
- Personal
Advanced Aliasing
Aliasing is a technique that allows you to use a certain type of bean multiple times in another bean. For example you can have two different users in a page object (author and editor). Aliasing is mostly used when you assign roles to people (think of database fields like leader_id, member_id). Example: Imagine we have a collection of people, some are family others are friends. To accomplish this in RedBeanPHP use:
class Alias extends RedBean_DefaultBeanFormatter {
public function getAlias($type) {
if ($type=='familyman' || $type=='buddy') return 'person';
return $type;
}
}
R::$writer->setBeanFormatter(new Alias);
list($p1,$p2,$p3) = R::dispense('person',3);
$p1->name = 'Joe';
$p2->name = 'Jack';
$p3->name = 'James';
$fm = R::dispense('familymember');
$fr = R::dispense('friend');
$fr->buddy = $p1;
$fm->familyman = $p2;
$p3->ownFamilymember[] = $fm;
$p3->ownFriend[] = $fr;
$id = R::store($p3);
$friend = R::load('person', $id);
echo reset($friend->ownFamilymember)->familyman->name; //Jack
echo reset($friend->ownFriend)->buddy->name; //Joe
You cannot use an Alias with R::view().
Aliasing does not work with nested collections of beans.
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 Disquspage generated in 0.0058169364929199 sec.