traverse depth info

已查看 23 次
跳至第一个未读帖子

Cesco

未读,
2016年7月26日 11:27:052016/7/26
收件人 redbeanphp
Hi,

Is there possibility to have the actual depth on traverse recursion?

Example:

$cane->traverse( 'ownCane', function( $cane ) {
     echo $actual_depth
;
});


gabor

未读,
2016年7月27日 17:50:132016/7/27
收件人 redbeanphp

Sorry, at this moment no, you can do it manually of course:

$level = 0;
$nodes
[0]->traverse( 'ownNode', function( $node ) use ( &$level ) {
        $level
++;
        echo $node
->name.$level;
} );



But that might not be what you're looking for.
Might be interesting to add as a new feature though.

cheers
Gabor

Cesco

未读,
2016年7月28日 04:42:152016/7/28
收件人 redbeanphp
Thanks Gabor!

but it's not exactly what i'm looking for because if i have more "sublevel" of "sublevel" i don't know what level the cycle is.
Example

|- Level 1 ($level = 1)
|-- Sub level 1 ($level = 2)
|-- Sub level 2 ($level = 3 wrong)
|--- Sub Sub level 1 ...
|-- Sub level 3  ...

I try to find another solution and waiting for an future update ;)

Regards

Cesco

未读,
2016年7月28日 11:22:562016/7/28
收件人 redbeanphp
I found this solution. I know that's orrible :( but it works :) ... I think there's a better way to to this... if someone want to help me... :)

    $property = 'cane';
    $bean = R::load($property,1);
    echo $bean->name.'<br />';
 
    $cats = array();

    $bean->traverse( 'own'.ucwords($property), function( $bean ) use ( &$property )  {
      global $cats;
      $level = 0;
      $cat = new stdClass();
      $cat->id = $bean->id;
      $cat->name = $bean->name;

      $getLevel = function ($bean) use (&$getLevel,&$level,&$cat,&$property) {
        if ($bean) {
          $level++;
          $getLevel($bean->$property);
        }
        else
          $cat->level = $level;
      };

      $getLevel($bean);
      $cats[] = $cat;
    });

gabor

未读,
2016年7月28日 12:42:132016/7/28
收件人 redbeanphp

Well, it's very creative :)

I will look into this, it's a lacking feature I agree.
Unfortunately, hierarchical queries are only supported by PostgreSQL, so the solution for other databases will always be a bit clunky.

cheers
Gabor

Cesco

未读,
2016年7月28日 15:18:282016/7/28
收件人 redbeanphp
Thanks very much. You have done a great work with RedBeanPHP and I hope you can introduce this feature on next update.

Francesco
回复全部
回复作者
转发
0 个新帖子