Self Referential

63 views
Skip to first unread message

Marcello Fontolan

unread,
Oct 24, 2014, 1:28:23 PM10/24/14
to redbe...@googlegroups.com
  Hi,

  Self Referential is something that I need in some cases to avoid many tables to do the same thing.
  For example, our application build the Application Menu from a table and I need a 3 level structure so, self referential is a need.
  In RedBean is a kind of mystery to me because I think about load a bean and list all children, at any level.

  I create a table with the following code:

$pcf1 = R::dispense('pcf');
$pcf1->item = '1';
$pcf1->descricao = 'Receitas';
R::store($pcf1);

$pcf2 = R::dispense('pcf');
$pcf2->item = '1.1';
$pcf2->descricao = 'Vendas';
$pcf2->pcf = $pcf1;
R::store($pcf2);

$pcf3 = R::dispense('pcf');
$pcf3->item = '1.1.1';
$pcf3->descricao = 'Depósito';
$pcf3->pcf = $pcf2;
R::store($pcf3);

$pcf3 = R::dispense('pcf');
$pcf3->item = '1.1.2';
$pcf3->descricao = 'Boleto';
$pcf3->pcf = $pcf2;
R::store($pcf3);

$pcf3 = R::dispense('pcf');
$pcf3->item = '1.1.3';
$pcf3->descricao = 'Dinheiro';
$pcf3->pcf = $pcf2;
R::store($pcf3); 

  When I access the data with the code:

$pcf1 = R::load('pcf', 1);
echo $pcf1->item . ' - ' . $pcf1->descricao . '<br>';
foreach ($pcf1->ownPcfList as $nv2) {
echo $nv2->item . ' - ' . $nv2->descricao . '<br>';
foreach ($nv2->ownPcfList as $nv3) {
echo $nv3->item . ' - ' . $nv3->descricao . '<br>';
}
}

  I have the output that I expect:

1 - Receitas
11 - Vendas
111 - Depósito
112 - Boleto
113 - Dinheiro


 But, if I try output the object with "print_r($pcf1->ownPcfList);" I have this "*RECURSION*" information.
  How is the correct way to get all childrens from a bean in a self referential table?
  How is the correct way to get a parent bean from a children in a self referential table?


Array
(
    [2] => RedBeanPHP\OODBBean Object
        (
            [properties:protected] => Array
                (
                    [id] => 2
                    [item] => 11
                    [descricao] => Vendas
                    [pcf_id] => 1
                )

            [__info:protected] => Array
                (
                    [type] => pcf
                    [sys.id] => id
                    [sys.orig] => Array
                        (
                            [id] => 2
                            [item] => 11
                            [descricao] => Vendas
                            [pcf_id] => 1
                        )

                    [tainted] => 
                    [changed] => 
                    [sys.parentcache.pcf] => RedBeanPHP\OODBBean Object
                        (
                            [properties:protected] => Array
                                (
                                    [id] => 1
                                    [item] => 1
                                    [descricao] => Receitas
                                    [pcf_id] => 
                                    [ownPcf] => Array
 *RECURSION*
                                )

                            [__info:protected] => Array
                                (
                                    [type] => pcf
                                    [sys.id] => id
                                    [sys.orig] => Array
                                        (
                                            [id] => 1
                                            [item] => 1
                                            [descricao] => Receitas
                                            [pcf_id] => 
                                        )

                                    [tainted] => 1
                                    [changed] => 
                                    [sys.shadow.ownPcf] => Array
                                        (
                                            [2] => RedBeanPHP\OODBBean Object
 *RECURSION*
                                        )

                                )

                            [beanHelper:protected] => RedBeanPHP\BeanHelper\SimpleFacadeBeanHelper Object
                                (
                                )

                            [fetchType:protected] => 
                            [withSql:protected] => 
                            [withParams:protected] => Array
                                (
                                )

                            [aliasName:protected] => 
                            [via:protected] => 
                            [noLoad:protected] => 
                            [all:protected] => 
                        )

                )

            [beanHelper:protected] => RedBeanPHP\BeanHelper\SimpleFacadeBeanHelper Object
                (
                )

            [fetchType:protected] => 
            [withSql:protected] => 
            [withParams:protected] => Array
                (
                )

            [aliasName:protected] => 
            [via:protected] => 
            [noLoad:protected] => 
            [all:protected] => 
        )

)


  Questions:
  1. How is the correct way to get all children from a bean in a self referential table?
  2. How is the correct way to get a parent bean from a children in a self referential table?


  Regards

  Marcello

gabor

unread,
Oct 25, 2014, 12:26:42 PM10/25/14
to redbe...@googlegroups.com

Hi,

The recursion detection is a feature of PHP.
If you want a clean list try:

print_r( $pcf1->export() );




To answer your questions:

1. To get all the children, you can use a loop like you do now or use the $bean->traverse() method: http://redbeanphp.com/trees

2. Given a child 'page' you can retrieve its parent (book) like this: $page->book, for self-referential beans this is exactly the same: $page->page.

cheers
Gabor

Coho

unread,
Nov 12, 2015, 3:55:01 AM11/12/15
to redbeanphp
Hi,

How do I go about obtaining all ancestors of a 'page' instead of just the parent without looping? Is there a method similar to traverse() that climbs up the tree instead of down? Perhaps you can make traverse work in both directions?

Thanks

gabor

unread,
Nov 16, 2015, 12:07:49 PM11/16/15
to redbeanphp

Sorry for my rather late reply (switching jobs over here), yes sure, you can traverse upwards as well. Example:

$page = R::dispense('page');
$page
->title = 'chapter';
$page2
= R::dispense('page');
$page2
->title = 'article';
$page3
= R::dispense('page');
$page3
->title = 'text';
$page
->ownPageList[] = $page2;
$page2
->ownPageList[] = $page3;
R
::store($page);

$p
= $page3->fresh();
$page3
->traverse('page', function($parent) {
        echo $parent
->title. PHP_EOL;
});


I'll add this example to the manual.

cheers
Gabor
Reply all
Reply to author
Forward
0 new messages