我的问题是使用PHP和MYSQL删除嵌套模型集中的节点的正确方法是什么。就我所关注的而言,我必须将所有左边的值减去大于节点以删除左边的值为2 ..但是我对后续步骤感到困惑。非常感谢您的回答。
这是我的功能:
public function deleteNode($id,$left,$right)
{
//?
}
答案 0 :(得分: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;
}
}
}