2011-06-12 106 views
0

我想在這裏使用了答案:Move node in nested set移動節點

要移動在MySQL中的節點組嵌套的分貝似乎除了移動節點設置爲負值好的工作和從不改變爲新的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); 
} 

我也有興趣,如果任何人知道如何使用它來向左或向右移動現有父級中的一個節點。

+0

發現這個問題有一個流氓我。 (0- $ iItemPosLeft)。'「和'rgt'> =」'。(0- $ iItemPosRight)。''');在最後的查詢中應該已經讀取: WHERE'lft' <=「'。 – Early73 2011-06-12 18:52:20

+0

仍然想知道是否有人可以幫助我使用它來移動當前父節點左側或右側的節點。謝謝! – Early73 2011-06-12 18:53:57

+0

要使用相同的查詢向左或向右移動節點,只需將想要移動節點出現的節點的左側值替換爲$ iParentPosRight值。 – Early73 2011-06-12 19:11:51

回答

0

我刪除了意外的我。並且查詢完全符合我的要求。要使用相同的查詢向左或向右移動節點,只需將想要移動節點出現的節點的左側值替換爲$ iParentPosRight值。