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

时间:2013-01-21 17:10:57

标签: php jquery mysql jstree nested-sets

我已经在我的网站上使用php / MySQL后端实现了jsTree,用于树存储和检索。我使用了jsTree下载的php / MySQL演示版,用于基础架构,然后根据我的需要进行了修改。

我已修改,以便多个树可以存储在同一个数据库中,并添加了一个新的“owner_id”列,用于存储创建该特定树的人的用户ID。

创建新分支或移动分支的php代码无法正常工作,因为它没有考虑到数据库中有多个树。

jsTree使用嵌套集模型,脚本调整数据库中所有树的左右值,而不仅仅是添加了新分支的树。这会慢慢破坏整个数据库。

以下代码显示了进行调整的函数,有人可以尝试为我修改代码,以便使用“owner_id”字段仅对特定树进行更改吗?

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

然后......

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;
    }
}

任何帮助都非常感激。

由于

1 个答案:

答案 0 :(得分:1)

对于其他任何可能有帮助的人,这是我的代码,我做的更改是为了使创建/移动节点仅适用于所讨论的特定树。

owner_id子句中添加where确实解决了这个问题。我需要将它添加到某些查询中:

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;
    }
}

希望它有所帮助...