【问题标题】:Move node in a nested set在嵌套集中移动节点
【发布时间】:2011-06-12 17:53:46
【问题描述】:

我正在尝试在这里使用答案: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);
}

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

【问题讨论】:

  • 发现这个问题有一个流氓我。在最后一个查询中应该是:WHERE lft rgt >= "'.(0-$iItemPosRight).'"');
  • 仍然想知道是否有人可以帮助我使用它在当前父节点内向左或向右移动节点。谢谢!
  • 要使用相同的查询向左或向右移动节点,只需将您希望移动节点出现在之前的节点的左侧值替换为 $iParentPosRight 值。

标签: mysql nested set move


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-29
    • 2013-10-07
    • 2021-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多