2

我的问题是使用 PHP 和 MYSQL 在嵌套模型集中删除节点的正确方法是什么。就我而言,我必须将所有大于节点的左值递减 2 以删除的左值。但是我对接下来的步骤很困惑。您的回答将不胜感激。

这是我的功能:

public function deleteNode($id,$left,$right)
{
    //?

}
4

1 回答 1

1

我刚刚从 Mike Hillyers 那里找到了解决方案

public function delete_node($id)
{

    $query = $this->db->query("
        SELECT node.label
        FROM menus AS node,menus AS parent
        WHERE node.lft BETWEEN parent.lft AND parent.rft AND parent.id = $id
        GROUP BY node.label
        ORDER BY node.lft
    ");

    if($query->num_rows() > 1)
    {
        $this->db->trans_start();
        $this->db->query("LOCK TABLE $this->table WRITE");
        $this->db->query("SELECT @myLeft := lft, @myRight := rft, @myWidth := rft - lft + 1 FROM $this->table WHERE id = $id");
        $this->db->query("DELETE FROM $this->table WHERE lft = @myLeft");
        $this->db->query("UPDATE $this->table SET rft = rft - 1, lft = lft - 1 WHERE lft BETWEEN @myLeft AND @myRight");
        $this->db->query("UPDATE $this->table SET rft = rft - 2 WHERE rft > @myRight");
        $this->db->query("UPDATE $this->table SET lft = lft - 2 WHERE lft > @myRight");
        $this->db->query("UNLOCK TABLES");
        $this->db->trans_complete();

        if ($this->db->trans_status() === FALSE)
        {
            return false;
        }
        else
        {
            return true;
        } 
    }
    else
    {
        $this->db->trans_start();
        $this->db->query("LOCK TABLE $this->table WRITE");
        $this->db->query("SELECT @myLeft := lft, @myRight := rft, @myWidth := rft - lft + 1 FROM $this->table WHERE id = $id");
        $this->db->query("DELETE FROM $this->table WHERE lft BETWEEN @myLeft AND @myRight");
        $this->db->query("UPDATE $this->table SET rft = rft - @myWidth WHERE rft > @myRight");
        $this->db->query("UPDATE $this->table SET lft = lft - @myWidth WHERE lft > @myRight");
        $this->db->query("UNLOCK TABLES");
        $this->db->trans_complete();

        if ($this->db->trans_status() === FALSE)
        {
            return false;
        }
        else
        {
            return true;
        }    
    }

}
于 2012-09-21T20:05:29.273 回答