在嵌套集中移动节点

时间:2011-06-12 17:53:47

标签: mysql nested set move

我正在尝试使用这里的答案:Move node in nested set

要在mysql嵌套集db中移动节点,它似乎工作正常,除了移动节点设置为负值并且永远不会更改为新的lft& rgt值。似乎步骤4根本没有做任何事情。我已经更改了脚本以匹配我的结构并删除了步骤4中的更新parentID,因为我没有将parentID存储在我的结构中。

有人可以告诉我为什么这不起作用吗?这是我的代码:

// id of moving node 
$iItemId = 13;

// left position of moving node
$iItemPosLeft = 19;

// right position of moving node
$iItemPosRight = 20;

// id of new parent node (where moving node should be moved)
$iParentId = 8;

// right position of new parent node (where moving node should be moved)
$iParentPosRight = 15;

// 'size' of moving node (including all its sub-nodes)
$iSize = $iItemPosRight - $iItemPosLeft + 1;

$sql = array(

// step 1: temporarily "remove" moving node

'UPDATE `RJF_storeNestedCategory`
SET `lft` = 0-(`lft`), `rgt` = 0-(`rgt`)
WHERE `lft` >= "'.$iItemPosLeft.'" AND `rgt` <= "'.$iItemPosRight.'"',

// step 2: decrease left and/or right position values of currently 'lower' items (and     parents)

'UPDATE `RJF_storeNestedCategory`
SET `lft` = `lft` - '.$iSize.'
WHERE `lft` > "'.$iItemPosRight.'"',
'UPDATE `RJF_storeNestedCategory`
SET `rgt` = `rgt` - '.$iSize.'
WHERE `rgt` > "'.$iItemPosRight.'"',

// step 3: increase left and/or right position values of future 'lower' items (and parents)

'UPDATE `RJF_storeNestedCategory`
SET `lft` = `lft` + '.$iSize.'
WHERE `lft` >= "'.($iParentPosRight > $iItemPosRight ? $iParentPosRight - $iSize : $iParentPosRight).'"',
'UPDATE `RJF_storeNestedCategory`
SET `rgt` = `rgt` + '.$iSize.'
WHERE `rgt` >= "'.($iParentPosRight > $iItemPosRight ? $iParentPosRight - $iSize : $iParentPosRight).'"',

// step 4: move node (ant it's subnodes) and update it's parent item id

'UPDATE `RJF_storeNestedCategory`
SET
    `lft` = 0-(`lft`)+'.($iParentPosRight > $iItemPosRight ? $iParentPosRight - $iItemPosRight - 1 : $iParentPosRight - $iItemPosRight - 1 + $iSize).',
    `rgt` = 0-(`rgt`)+'.($iParentPosRight > $iItemPosRight ? $iParentPosRight - $iItemPosRight - 1 : $iParentPosRight - $iItemPosRight - 1 + $iSize).'
WHERE `lft` <= "'.(0-$iItemPosLeft).'" AND i.`rgt` >= "'.(0-$iItemPosRight).'"');


foreach($sql as $sqlQuery){
  mysql_query($sqlQuery);
}

如果有人知道如何使用它在现有父节点内向左或向右移动节点,我也很感兴趣。

1 个答案:

答案 0 :(得分:0)

我删除了意外的我。而查询完全符合我的要求。要使用相同的查询向左或向右移动节点,只需将您希望移动节点出现的节点的左值替换为$ iParentPosRight值。