Trees

RedBeanPHP also supports self-referential one-to-many and many-to-many relationships. In RedBeanPHP terminology these are called trees. Here is an example, let's decorate a christmas tree with some candy canes:


    $cane 
R::dispense('cane',10);
    
$cane[1]->ownCane = array( $cane[2], $cane[9] );
    
$cane[2]->ownCane = array( $cane[3], $cane[4] );
    
$cane[4]->ownCane = array( $cane[5], 
                
$cane[7], $cane[8] );
    
$cane[5]->ownCane = array( $cane[6] );
    
$id R::store($cane[1]);
    
$root R::load('cane',$id);
    echo 
$root->ownCane[2]->ownCane[4]
        ->
ownCane[5]->ownCane[6]->id;
    
//outputs: 6

In fact trees are just a special case of lists.

In other words…

A Tree is the RedBeanPHP version of a self-referential relationship.


 
 

RedBeanPHP Easy ORM for PHP © 2013 Gabor de Mooij and the RedBeanPHP community - Licensed New BSD/GPLv2