Advanced Associations

From RedBean

Jump to: navigation, search

Contents

Associations

Beans can be related; for instance a book has pages; a post may have comments etc. RedBean ships with an AssociationManager that can handle associations.

Simple relations: links

To associate an author with a book; you can use a so-called foreign key. In this case each book has exactly ONE author (but an author can have many books). Therefore this type of relation is sometimes called N-1 relation. RedBean supports foreign keys; given a bean $book and a bean $author with id 5 you can write:

$linker = new RedBean_LinkManager( $toolbox );
$linker->link( $book, $author );
//creates author_id = 5

To get the bean back simply say:

$author = $linker->getBean( $book, "author" );

To break the link between a book and it's author:

$linker->breakLink( $book, 'author' );

Sometimes you need a slightly different kind of association. Take for instance an address item. You might decide that you want to distinguish between a postal and a home address. This means that a person has two addresses, but no more. This is not really N-1, but rather 2-1. We call this X-1 relations. To create an X-1 relationship between address and it's owner you simply add the key-name as a third parameter. Let's look at an example.

$linker->link($person, $homeAddress, 'home'); 
$linker->link($person, $postalAddress, 'postal'); 

To fetch these different beans:

$homeAddress = $linker->getBean($person, 'address', 'home'); 
$postalAddress = $linker->getBean($person, 'address', 'postal'); 

To break this kind of link:

$linker->breakLink( $person, 'address', 'postal' );


API Class Reference: RedBean_LinkManager



Creating the Association Manager

The goal of the association manager is to manage associated beans. In order to do so, it needs a Toolbox. To get an instance of the manager class use:

$a = new RedBean_AssociationManager( $toolbox );

Connecting two Beans

To create a connection between two beans, just associate them:

$a->associate($page, $user);

The $page and the $user are now associated. In RedBean you can associate anything with anything. All required tables will be generated on the fly for you.

Getting Related Beans

To get all pages associated with $user, type:

$keys = $a->related($user, "page" );

This will return an array containing the primary keys of every page related to $user. You can also get the page beans directly if you like. To accomplish this we need to use the batch loader from the RedBean Core class.

$pages = ($redbean->batch("page", $a->related($user, "page" )));

Breaking the connection between Beans

To clear the association between $page and $user type:

$a->unassociate($page, $user);

To clear all related pages:

$a->clearRelations($user, "page");

Cascading Deletes

To enforce cascading deletes:

RedBean_Plugin_Constraint::addConstraint($page, $user);
$redbean->trash($page) now causes the association to break automatically.

API Class Reference: RedBean_AssociationManager

Trees

First Create a Tree Manager and give him the toolbox.

$tree = new RedBean_TreeManager( $toolbox );

To attach a child bean to a parent bean say:

$tree->attach( $parent, $child );

To get all the children under a parent:

$pages = $tree->children( $parent );

To get the parent id of a child bean, simply access the property parent_id:

$child->parent_id


API Class Reference: RedBean_TreeManager


Extended Associations

Note that an association is in fact also just a bean; it only has a different name based on the names of the beans to be associated. For instance associating a page with a book results in a bean of type book_page (alphabetically ordered). With normal associations you do not have any control over this association bean. However if you want to qualify associations (in our example; order the pages of a book) then you can use the extended association manager:

$ea = new RedBean_ExtAssociationManager( $toolbox );
$pageNumber = $redbean->dispense("pagenumber");
$pageNumber->number = 9;
$ea->extAssociate( $page, $book, $pageNumber );

Also see: Toolbox,Fuse,Beans,Tutorial

Personal tools