Answer the question
In order to leave comments, you need to log in
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
public function move($parentId, $leftSibling = null)
Category::find(195)->move(199);
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
$parent = self::find($parentId);
3. The right key of the node for which we insert the node (branch):
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;
$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');
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 . ')')));
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question