2016-03-13 129 views
1

我現在用的嵌套插件類商鋪http://dbushell.github.io/Nestable/jQuery的嵌套 - 保存到MySQL和PHP嵌套集模型

我保存使用Nested Set modelparent_id, order, level數據到MySQL數據庫。

但我想使用嵌套的模型只用列lft, rgt

請你給我的例子,如何將數據保存到數據庫或者是有,我可以用它來只需要插入新項目的任何包裝,更新所有的樹並刪除一個項目,使用嵌套集模型


我發現猴麪包樹庫http://www.sideralis.org/baobab/,其執行這些操作,但它是非常沉重的。

回答

1

這裏......

要插入:

begin; 
SELECT rgt INTO new_lft FROM tree_map WHERE node_id = parent_id; 
UPDATE tree_map SET rgt = rgt + 2 WHERE rgt >= new_lft; 
UPDATE tree_map SET lft = lft + 2 WHERE lft > new_lft; 
INSERT INTO tree_map (lft, rgt, parent_id) VALUES (new_lft, (new_lft + 1), parent_id); 
commit; 
SELECT LAST_INSERT_ID(); 

刪除:

SELECT lft, rgt, (rgt - lft), (rgt - lft + 1), parent_id 
    INTO new_lft, new_rgt, has_leafs, width, superior_parent 
    FROM tree_map WHERE node_id = pnode_id; 

begin; 
DELETE FROM tree_content WHERE node_id = pnode_id; 

IF (has_leafs = 1) THEN 
    DELETE FROM tree_map WHERE lft BETWEEN new_lft AND new_rgt; 
    UPDATE tree_map SET rgt = rgt - width WHERE rgt > new_rgt; 
    UPDATE tree_map SET lft = lft - width WHERE lft > new_rgt; 
ELSE 
    DELETE FROM tree_map WHERE lft = new_lft; 
    UPDATE tree_map SET rgt = rgt - 1, lft = lft - 1, parent_id = superior_parent 
    WHERE lft BETWEEN new_lft AND new_rgt; 
    UPDATE tree_map SET rgt = rgt - 2 WHERE rgt > new_rgt; 
    UPDATE tree_map SET lft = lft - 2 WHERE lft > new_rgt; 
END IF; 
commit; 

你也可能需要使用驚人的存儲過程的MySQL,而不:

https://github.com/werc/TreeTraversal/blob/master/sql/r_tree_traversal.sql#L4