The Power ORM


Sometimes you want to refer to a bean using a different name. For instance, when you have a course referring to a teacher and a student, both of which are people. In this case you can use fetchAs:

    $c R::dispense'course' );

//At assignment time, no difference...
$c->teacher R::dispense'person' );
$c->student R::dispense'person' );

$id R::store$c );
$c R::load'course'$id );

//when accessing the aliased properties,
    //tell RedBeanPHP how to find the bean:
$teacher $c->fetchAs'person' )->teacher;

fetchAs tells RedBeanPHP the ID has to be associated with a different type (in this case 'person' instead of 'teacher' or 'student'). This also works the other way:

    //returns all courses for this person
    //where he/she is the teacher.
$person->alias'teacher' )->ownCourseList;

From a relational point of view, we have exactly two people for every row (although one or both can be NULL of course). This is why we call these 'aliases' one-to-X relations (or one-to-fixed relations), where X is a fixed number.
You can use as many of these 'aliases' as you like.

As of 4.2 you can use global aliases. Instead of specifying the alias with fetchAs each time you can specify the alias using R::aliases( ...aliases... ); i.e. R::aliases( ['teacher' => 'person', and so on...] );

Also learn about Many-to-many relations.

back to main menu

Performance monitor: this page has been generated in 0.028685092926025s. Is the performance lacking? Please drop me an e-mail to notify me!



RedBeanPHP Easy ORM for PHP © 2021 () and the RedBeanPHP community () - Licensed New BSD/GPLv2 - RedBeanPHP Archives
RedBeanPHP, the power ORM for PHP since 2009.

Privacy Statement