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:
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?
[2] => RedBeanPHP\OODBBean Object
[properties:protected] => Array
[__info:protected] => Array
[sys.parentcache.pcf] => RedBeanPHP\OODBBean Object
[properties:protected] => Array
[__info:protected] => Array
[sys.shadow.ownPcf] => Array
[2] => RedBeanPHP\OODBBean Object
[beanHelper:protected] => RedBeanPHP\BeanHelper\SimpleFacadeBeanHelper Object
[withParams:protected] => Array
[beanHelper:protected] => RedBeanPHP\BeanHelper\SimpleFacadeBeanHelper Object
[withParams:protected] => Array
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