M
M
Muhammad2015-05-22 21:42:22
nested set
Muhammad, 2015-05-22 21:42:22

Moving Nested Set branch in Laravel?

Good evening. I'm trying to move the NS branch on this article: www.getinfo.ru/article610.html.

To begin with, let's select the keys of the following nodes:
1. Keys and the level of the node being moved;
SELECT level, left_key, right_key FROM my_tree WHERE id = $id

This is how I do it:
public function move($parentId, $leftSibling = null)

Category::find(195)->move(199);
. where 195 is the id of the node being moved, 199 is the id of the new parent.
2. The level of the new parent node (if the node is moved to the "root" then you can immediately substitute the value 0):
SELECT level FROM my_tree WHERE id = $id_up Get
$level_up

I select the parent as a whole:
$parent = self::find($parentId);
3. The right key of the node for which we insert the node (branch):

I do it like this:
if ($this->parentNode()->id == $parent->id)
    {
      if ($parent->hasChild())
      {
        if ($leftSibling === null)
        {
          throw Exception('Sibling not found');
        }

        $rightKeyNear = $leftSibling->right_key;
      }
      else
      {
        $rightKeyNear = $parent->left_key;
      }
    }
    else
    {
      if ($parent->level == 0)
      {
        //перемещение в корень
        $rightKeyNear = self::max('right_key')->get();
      }
      else
      {
        $rightKeyNear = $parent->right_key - 1;
      }
    }

4. Define the offsets:
$level_up - $level + 1 = $skew_level - offset of the level of the node being changed;
$right_key - $left_key + 1 = $skew_tree - tree key offset;

I have $keySkew instead of $skew_tree:
$levelSkew = $parent->level - $this->level + 1;
$keySkew = $this->right_key - $this->left_key + 1;

Select all nodes of the moved branch:
SELECT id FROM my_tree WHERE left_key >= $left_key AND right_key <= $right_key Get
$id_edit - list of id numbers of the moved branch.

$childIds = self::where('left_key', '>=', $this->left_key)->where('right_key', '<=', $this->right_key)->lists('id');

It is also required to determine which area the node is moving to, for this we compare $right_key and $right_key_near, if $right_key_near is greater, then the node moves to the region of higher nodes, otherwise - lower ones (why there is a separation is described below).
if ($rightKeyNear > $this->right_key)
    {
      echo 'up';
      $editSkew = $rightKeyNear - $this->left_key + 1;

      // UPDATE my_tree SET right_key = right_key + $skew_tree WHERE right_key < $left_key AND right_key > $right_key_near
      self::where('right_key', '<', $this->left_key)->where('right_key', '>', $rightKeyNear)->update(array('right_key' => DB::raw('`right_key` + (' . $keySkew . ')')));
  
      // UPDATE my_tree SET left_key = left_key + $skew_tree WHERE left_key < $left_key AND left_key > $right_key_near
      self::where('left_key', '<', $this->left_key)->where('left_key', '>', $rightKeyNear)->update(array('left_key' => DB::raw('`left_key` - (' . $keySkew . ')')));
      
    }
    else
    {
      echo 'down';
      $editSkew = $rightKeyNear - $this->left_key - $keySkew + 1;

      // UPDATE my_tree SET right_key = right_key - $skew_tree WHERE right_key > $right_key AND right_key <= $right_key_near
      self::where('right_key', '>', $this->right_key)->where('right_key', '<=', $rightKeyNear)->update(array('right_key' => DB::raw('`right_key` - (' . $keySkew . ')')));
    
      // UPDATE my_tree SET left_key = left_key - $skew_tree WHERE left_key < $left_key AND left_key > $right_key_near
      self::where('left_key', '<', $this->left_key)->where('left_key', '>', $rightKeyNear)->update(array('left_key' => DB::raw('`left_key` - (' . $keySkew . ')')));
    }

// UPDATE my_tree SET left_key = left_key + $skew_edit, right_key = right_key + $skew_edit, level = level + $skew_level WHERE id IN ($id_edit)
    self::whereIn('id', $childIds)->update(array('left_key' => DB::raw('`left_key` + (' . $editSkew . ')'), 'right_key' => DB::raw('`right_key` + (' . $editSkew . ')'), 'level' => DB::raw('`level` + (' . $levelSkew . ')')));

Whole code: pastebin.com/t9BJ2tDr
The problem is that for some reason the branch is not moving. Please point out any mistakes.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rikcon, 2015-05-22
@Rikcon

https://github.com/etrepat/baum
Look at the code for this package, I am currently using it for Nested Sets (Web Store Category Tree).
Everything is working.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question