dongqing7789 2013-01-21 17:10
浏览 100
已采纳

jsTree多树MySQL / PHP实现 - 嵌套集

I have implemented jsTree on my site with a php/MySQL back-end for tree storage and retrieval. I used the php/MySQL demo that came with the jsTree download for the basic infrastructure and then modified to my needs.

I have modified so that multiple trees can be stored in the same database, and added a new column of "owner_id" that stores the userid of the person that created that particular tree.

The php code that creates a new branch or moves a branch is not working correctly as it is not taking into account that there are multiple trees within the database.

jsTree uses the nested set model, and the script is adjusting the left and right values of all the trees in the database instead of just the one that has had a new branch added. This is slowly corrupting the entire database.

The following code shows the function/s that does the adjusting, could someone please try and amend the code for me so it uses the "owner_id" field to only make the changes to a particular tree?

function _create($parent, $position) {
    return $this->_move(0, $parent, $position);
}

and then...

function _move($id, $ref_id, $position = 0, $is_copy = false) {
    $hbhbhbh = fSession::get('nodes_allowed[nodes_access]');
    if ($hbhbhbh == "0" || $hbhbhbh == "2" || $hbhbhbh == "3") {
    if((int)$ref_id === 0 || (int)$id === 1) { return false; }
    $sql        = array();                      // Queries executed at the end
    $node       = $this->_get_node_ifuueuwyhddd($id);       // Node data
    $nchildren  = $this->_get_children($id);    // Node children
    $ref_node   = $this->_get_node_ifuueuwyhddd($ref_id);   // Ref node data
    $rchildren  = $this->_get_children($ref_id);// Ref node children

    $ndif = 2;
    $node_ids = array(-1);
    if($node !== false) {
        $node_ids = array_keys($this->_get_children($id, true));
        // TODO: should be !$is_copy && , but if copied to self - screws some right indexes
        if(in_array($ref_id, $node_ids)) return false;
        $ndif = $node[$this->fields["right"]] - $node[$this->fields["left"]] + 1;
    }
    if($position >= count($rchildren)) {
        $position = count($rchildren);
    }

    // Not creating or copying - old parent is cleaned
    if($node !== false && $is_copy == false) {
        $sql[] = "" . 
            "UPDATE `".$this->table."` " . 
                "SET `".$this->fields["position"]."` = `".$this->fields["position"]."` - 1 " . 
            "WHERE " . 
                "`".$this->fields["parent_id"]."` = ".$node[$this->fields["parent_id"]]." AND " . 
                "`".$this->fields["position"]."` > ".$node[$this->fields["position"]];
        $sql[] = "" . 
            "UPDATE `".$this->table."` " . 
                "SET `".$this->fields["left"]."` = `".$this->fields["left"]."` - ".$ndif." " . 
            "WHERE `".$this->fields["left"]."` > ".$node[$this->fields["right"]];
        $sql[] = "" . 
            "UPDATE `".$this->table."` " . 
                "SET `".$this->fields["right"]."` = `".$this->fields["right"]."` - ".$ndif." " . 
            "WHERE " . 
                "`".$this->fields["right"]."` > ".$node[$this->fields["left"]]." AND " . 
                "`".$this->fields["id"]."` NOT IN (".implode(",", $node_ids).") ";
    }
    // Preparing new parent
    $sql[] = "" . 
        "UPDATE `".$this->table."` " . 
            "SET `".$this->fields["position"]."` = `".$this->fields["position"]."` + 1 " . 
        "WHERE " . 
            "`".$this->fields["parent_id"]."` = ".$ref_id." AND " . 
            "`".$this->fields["position"]."` >= ".$position." " . 
            ( $is_copy ? "" : " AND `".$this->fields["id"]."` NOT IN (".implode(",", $node_ids).") ");

    $ref_ind = $ref_id === 0 ? (int)$rchildren[count($rchildren) - 1][$this->fields["right"]] + 1 : (int)$ref_node[$this->fields["right"]];
    $ref_ind = max($ref_ind, 1);

    $self = ($node !== false && !$is_copy && (int)$node[$this->fields["parent_id"]] == $ref_id && $position > $node[$this->fields["position"]]) ? 1 : 0;
    foreach($rchildren as $k => $v) {
        if($v[$this->fields["position"]] - $self == $position) {
            $ref_ind = (int)$v[$this->fields["left"]];
            break;
        }
    }
    if($node !== false && !$is_copy && $node[$this->fields["left"]] < $ref_ind) {
        $ref_ind -= $ndif;
    }

    $sql[] = "" . 
        "UPDATE `".$this->table."` " . 
            "SET `".$this->fields["left"]."` = `".$this->fields["left"]."` + ".$ndif." " . 
        "WHERE " . 
            "`".$this->fields["left"]."` >= ".$ref_ind." " . 
            ( $is_copy ? "" : " AND `".$this->fields["id"]."` NOT IN (".implode(",", $node_ids).") ");
    $sql[] = "" . 
        "UPDATE `".$this->table."` " . 
            "SET `".$this->fields["right"]."` = `".$this->fields["right"]."` + ".$ndif." " . 
        "WHERE " . 
            "`".$this->fields["right"]."` >= ".$ref_ind." " . 
            ( $is_copy ? "" : " AND `".$this->fields["id"]."` NOT IN (".implode(",", $node_ids).") ");

    $ldif = $ref_id == 0 ? 0 : $ref_node[$this->fields["level"]] + 1;
    $idif = $ref_ind;
    if($node !== false) {
        $ldif = $node[$this->fields["level"]] - ($ref_node[$this->fields["level"]] + 1);
        $idif = $node[$this->fields["left"]] - $ref_ind;
        if($is_copy) {
            $sql[] = "" . 
                "INSERT INTO `".$this->table."` (" .
                    "`".$this->fields["parent_id"]."`, " . 
                    "`".$this->fields["position"]."`, " . 
                    "`".$this->fields["left"]."`, " . 
                    "`".$this->fields["right"]."`, " . 
                    "`".$this->fields["level"]."`" . 
                ") " . 
                    "SELECT " .
                        "".$ref_id.", " . 
                        "`".$this->fields["position"]."`, " . 
                        "`".$this->fields["left"]."` - (".($idif + ($node[$this->fields["left"]] >= $ref_ind ? $ndif : 0))."), " . 
                        "`".$this->fields["right"]."` - (".($idif + ($node[$this->fields["left"]] >= $ref_ind ? $ndif : 0))."), " . 
                        "`".$this->fields["level"]."` - (".$ldif.") " . 
                    "FROM `".$this->table."` " . 
                    "WHERE " . 
                        "`".$this->fields["id"]."` IN (".implode(",", $node_ids).") " . 
                    "ORDER BY `".$this->fields["level"]."` ASC";
        }
        else {
            $sql[] = "" . 
                "UPDATE `".$this->table."` SET " . 
                    "`".$this->fields["parent_id"]."` = ".$ref_id.", " . 
                    "`".$this->fields["position"]."` = ".$position." " . 
                "WHERE " . 
                    "`".$this->fields["id"]."` = ".$id;
            $sql[] = "" . 
                "UPDATE `".$this->table."` SET " . 
                    "`".$this->fields["left"]."` = `".$this->fields["left"]."` - (".$idif."), " . 
                    "`".$this->fields["right"]."` = `".$this->fields["right"]."` - (".$idif."), " . 
                    "`".$this->fields["level"]."` = `".$this->fields["level"]."` - (".$ldif.") " . 
                "WHERE " . 
                    "`".$this->fields["id"]."` IN (".implode(",", $node_ids).") ";
        }
    }
    else {
        $ewre = fSession::get('user[user_id]');
        $sql[] = "" . 
            "INSERT INTO `".$this->table."` (" .
                "`".$this->fields["owner"]."`, " . 
                "`".$this->fields["parent_id"]."`, " . 
                "`".$this->fields["position"]."`, " . 
                "`".$this->fields["left"]."`, " . 
                "`".$this->fields["right"]."`, " . 
                "`".$this->fields["level"]."` " . 
                ") " . 
            "VALUES (" .
                $ewre.", " .
                $ref_id.", " . 
                $position.", " . 
                $idif.", " . 
                ($idif + 1).", " . 
                $ldif. 
            ")";
    }
    foreach($sql as $q) { $this->db->query($q); }
    $ind = $this->db->insert_id();
    if($is_copy) $this->_fix_copy($ind, $position);
    return $node === false || $is_copy ? $ind : true;
    }
}

Any help really appreciated.

Thanks

  • 写回答

1条回答 默认 最新

  • dongpan9760 2013-01-22 17:13
    关注

    For anyone else this might help, here is my code after the changes to I made to make creating / moving nodes only apply to the particular tree in question.

    Adding the owner_id in the where clause did in fact fix the issue. I needed to add it to only certain queries:

    function _move($id, $ref_id, $position = 0, $is_copy = false) {
        $hbhbhbh = fSession::get('nodes_allowed[nodes_access]');
        if ($hbhbhbh == "0" || $hbhbhbh == "2" || $hbhbhbh == "3") {
        if((int)$ref_id === 0 || (int)$id === 1) { return false; }
        $sql        = array();                      // Queries executed at the end
        $node       = $this->_get_node_ifuueuwyhddd($id);       // Node data
        $nchildren  = $this->_get_children($id);    // Node children
        $ref_node   = $this->_get_node_ifuueuwyhddd($ref_id);   // Ref node data
        $rchildren  = $this->_get_children($ref_id);// Ref node children
    
    
        $ndif = 2;
        $node_ids = array(-1);
    
        if($node !== false) {
    
    
            $node_ids = array_keys($this->_get_children($id, true));
            // TODO: should be !$is_copy && , but if copied to self - screws some right indexes
            if(in_array($ref_id, $node_ids)) return false;
            $ndif = $node[$this->fields["right"]] - $node[$this->fields["left"]] + 1;
        }
    
        if($position >= count($rchildren)) {        
    
            $position = count($rchildren);
        }
    
    
        // Not creating or copying - old parent is cleaned
        if($node !== false && $is_copy == false) {
    
    
            $sql[] = "" . 
                "UPDATE `".$this->table."` " . 
                    "SET `".$this->fields["position"]."` = `".$this->fields["position"]."` - 1 " . 
                "WHERE " .
                    "`".$this->fields["owner"]."` = ".(int) $node[$this->fields["owner"]]." AND " .
                    "`".$this->fields["parent_id"]."` = ".$node[$this->fields["parent_id"]]." AND " . 
                    "`".$this->fields["position"]."` > ".$node[$this->fields["position"]];
            $sql[] = "" . 
                "UPDATE `".$this->table."` " . 
                    "SET `".$this->fields["left"]."` = `".$this->fields["left"]."` - ".$ndif." " . 
                "WHERE `".$this->fields["left"]."` > ".$node[$this->fields["right"]]." AND " .
                    "`".$this->fields["owner"]."` = ".(int) $node[$this->fields["owner"]];
            $sql[] = "" . 
                "UPDATE `".$this->table."` " . 
                    "SET `".$this->fields["right"]."` = `".$this->fields["right"]."` - ".$ndif." " . 
                "WHERE " .
                    "`".$this->fields["owner"]."` = ".(int) $node[$this->fields["owner"]]." AND " .
                    "`".$this->fields["right"]."` > ".$node[$this->fields["left"]]." AND " . 
                    "`".$this->fields["id"]."` NOT IN (".implode(",", $node_ids).") ";
        }
    
    
    
        $sql[] = "" . 
            "UPDATE `".$this->table."` " . 
                "SET `".$this->fields["position"]."` = `".$this->fields["position"]."` + 1 " . 
            "WHERE " . 
                "`".$this->fields["parent_id"]."` = ".$ref_id." AND " . 
                "`".$this->fields["position"]."` >= ".$position." " . 
                ( $is_copy ? "" : " AND `".$this->fields["id"]."` NOT IN (".implode(",", $node_ids).") ");
    
    
    
        $ref_ind = $ref_id === 0 ? (int)$rchildren[count($rchildren) - 1][$this->fields["right"]] + 1 : (int)$ref_node[$this->fields["right"]];
        $ref_ind = max($ref_ind, 1); 
    
    
        $self = ($node !== false && !$is_copy && (int)$node[$this->fields["parent_id"]] == $ref_id && $position > $node[$this->fields["position"]]) ? 1 : 0;
    
        foreach($rchildren as $k => $v) {
    
    
            if($v[$this->fields["position"]] - $self == $position) {
                $ref_ind = (int)$v[$this->fields["left"]];
                break;
            }
        }
    
    
        if($node !== false && !$is_copy && $node[$this->fields["left"]] < $ref_ind) {
    
    
    
            $ref_ind -= $ndif;
        }
    
        $sql[] = "" . 
            "UPDATE `".$this->table."` " . 
                "SET `".$this->fields["left"]."` = `".$this->fields["left"]."` + ".$ndif." " . 
            "WHERE " . 
                "`".$this->fields["owner"]."` = ".(int) $ref_node[$this->fields["owner"]]." AND `".$this->fields["left"]."` >= ".$ref_ind." " . 
                ( $is_copy ? "" : " AND `".$this->fields["id"]."` NOT IN (".implode(",", $node_ids).") ");
        $sql[] = "" . 
            "UPDATE `".$this->table."` " . 
                "SET `".$this->fields["right"]."` = `".$this->fields["right"]."` + ".$ndif." " . 
            "WHERE " . 
                "`".$this->fields["owner"]."` = ".(int) $ref_node[$this->fields["owner"]]." AND `".$this->fields["right"]."` >= ".$ref_ind." " . 
                ( $is_copy ? "" : " AND `".$this->fields["id"]."` NOT IN (".implode(",", $node_ids).") ");
    
    
        $ldif = $ref_id == 0 ? 0 : $ref_node[$this->fields["level"]] + 1;
        $idif = $ref_ind;
    
    
        if($node !== false) {
    
    
            $ldif = $node[$this->fields["level"]] - ($ref_node[$this->fields["level"]] + 1);
            $idif = $node[$this->fields["left"]] - $ref_ind;
            if($is_copy) {
    
    
                $sql[] = "" . 
                    "INSERT INTO `".$this->table."` (" .
                        "`".$this->fields["parent_id"]."`, " . 
                        "`".$this->fields["position"]."`, " . 
                        "`".$this->fields["left"]."`, " . 
                        "`".$this->fields["right"]."`, " . 
                        "`".$this->fields["level"]."`" . 
                    ") " . 
                        "SELECT " .
                            "".$ref_id.", " . 
                            "`".$this->fields["position"]."`, " . 
                            "`".$this->fields["left"]."` - (".($idif + ($node[$this->fields["left"]] >= $ref_ind ? $ndif : 0))."), " . 
                            "`".$this->fields["right"]."` - (".($idif + ($node[$this->fields["left"]] >= $ref_ind ? $ndif : 0))."), " . 
                            "`".$this->fields["level"]."` - (".$ldif.") " . 
                        "FROM `".$this->table."` " . 
                        "WHERE " . 
                            "`".$this->fields["id"]."` IN (".implode(",", $node_ids).") " . 
                        "ORDER BY `".$this->fields["level"]."` ASC";
            }
            else {
                $sql[] = "" . 
                    "UPDATE `".$this->table."` SET " . 
                        "`".$this->fields["parent_id"]."` = ".$ref_id.", " . 
                        "`".$this->fields["position"]."` = ".$position." " . 
                    "WHERE " . 
                        "`".$this->fields["id"]."` = ".$id;
                $sql[] = "" . 
                    "UPDATE `".$this->table."` SET " . 
                        "`".$this->fields["left"]."` = `".$this->fields["left"]."` - (".$idif."), " . 
                        "`".$this->fields["right"]."` = `".$this->fields["right"]."` - (".$idif."), " . 
                        "`".$this->fields["level"]."` = `".$this->fields["level"]."` - (".$ldif.") " . 
                    "WHERE " . 
                        "`".$this->fields["id"]."` IN (".implode(",", $node_ids).") ";
            }
    
        } else {
    
    
            $ewre = fSession::get('user[user_id]');
            $sql[] = "" . 
                "INSERT INTO `".$this->table."` (" .
                    "`".$this->fields["owner"]."`, " . 
                    "`".$this->fields["parent_id"]."`, " . 
                    "`".$this->fields["position"]."`, " . 
                    "`".$this->fields["left"]."`, " . 
                    "`".$this->fields["right"]."`, " . 
                    "`".$this->fields["level"]."` " . 
                    ") " . 
                "VALUES (" .
                    $ewre.", " .
                    $ref_id.", " . 
                    $position.", " . 
                    $idif.", " . 
                    ($idif + 1).", " . 
                    $ldif. 
                ")";
        }
    
    
        foreach($sql as $q) { $this->db->query($q); }
        $ind = $this->db->insert_id();
        if($is_copy) $this->_fix_copy($ind, $position);
        return $node === false || $is_copy ? $ind : true;
        }
    }
    

    Hope it helps someone...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 数学的三元一次方程求解
  • ¥20 iqoo11 如何下载安装工程模式
  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题