Code snippets for symfony 1.x

Navigation

Refine Tags

Snippets tagged "sfpropelactasnestedsetbehaviorplugin"

Tree Rebuild for sfPropelActAsNestedSetBehaviorPlugin plugin

Tree managed by sfPropelActAsNestedSetBehaviorPlugin plugin can be corrupted. In this case, the left & right value are not well numbered (there is hole in numbering) then some method like retrieveFirstChild() or hasChildren() can return incorrect results.

This is a method that scan all the tree and correct left & right numbering. It modify the left & right number only if necessary. It make at maximum one Select SQL request and one update SQL request by node.

It can work for any subtree of the given node or for entire tree if you give the root node as parameter. Second parameter indicate from which number, the numbering start.

  public static function rebuildSubTree($base_node,$number=1)
  {
    $left=$number;
    $children=$base_node->getChildren();
    if (is_array($children))
    {
      foreach ($children as $child)
      {
        $number=self::rebuildSubTree($child,$number+1);
      }
    }
    $right=$number+1;
    if($base_node->getLeftValue()!=$left || $base_node->getRightValue()!=$right)
    {
      $base_node->setLeftValue($left);
      $base_node->setRightValue($right);
      $base_node->save();
    }
    return $number+1;
  }
 
by Sylvain PAPET on 2008-02-13, tagged plugin  rebuild  sfpropelactasnestedsetbehaviorplugin  tree