doulou9927 2015-06-08 09:20
浏览 115
已采纳

使用嵌套集模型在sqlite中存储分层数据如何将类别移动到另一个类别

I'm trying to store hierarchical data using SQLite. After searching a lot, I chose to use the nested set model instead of an adjacency list, because almost 90% of the operations will be reads and only 10% will be updates/deletes/creates.

I followed this example: http://www.phpro.org/tutorials/Managing-Hierarchical-Data-with-PHP-and-MySQL.html

And it works fine to add, delete and read new nodes.

But I didn't find any article explaining how to update the tree, e.g. moving a category into another category.

Below is my database structure:

id   name   left_node   right_node
1    name1     1          2

** I didn't find a place explaining how to update the hierarchy, which I really need. **

Another problem is

public function delete_node($pleft, $pright){

$width = $pright-$pleft+1;

$delete_sql = "delete from categories where left_node between $pleft and $pright";
$update_sql1 = "update categories set right_node = right_node-$width where right_node > $pright";
$update_sql2 = "update categories set left_node = left_node-$width where left_node> $pright";
//
$this->db->trans_start();
//
$this->db->query($delete_sql);

//
$this->db->query($update_sql1);
$this->db->query($update_sql2);
$this->db->trans_complete();
//
return $this->db->trans_status();
}

This my delete method, and it takes 30ms to finish. Is that normal?

I solved the problem, thanks for the help http://www.ninthavenue.com.au/how-to-move-a-node-in-nested-sets-with-sql

I'm using codeigniter with sqlite database. below is my function,

public function move_node($pleft, $pright, $origin_left_pos, $origin_right_pos){

//
//the new_left_position is different according to which way you want to move the node 
$new_left_position = $pleft + 1;
//
$width = $origin_right_pos - $origin_left_pos + 1;
$temp_left_position = $origin_left_pos;

$distance = $new_left_position - $origin_left_pos;
//backwards movement must account for new space
if($distance < 0){

  $distance -= $width;
  $temp_left_position += $width;

}
//
$update_sql1 = "update categories set left_node = left_node+$width where left_node >=  $new_left_position";

$update_sql2 = "update categories set right_node = right_node+$width where right_node >= $new_left_position";

//
$update_sql3 = "update categories set left_node = left_node+$distance , right_node = right_node+$distance where left_node >= $temp_left_position AND right_node < $temp_left_position+$width";

//
$update_sql4 = "update categories set left_node = left_node-$width where left_node > $origin_right_pos";
$update_sql5 = "update categories set right_node = right_node-$width where right_node > $origin_right_pos";

//

$this->db->trans_start();

$this->db->query($update_sql1);

//
$this->db->query($update_sql2);
$this->db->query($update_sql3);
$this->db->query($update_sql4);
$this->db->query($update_sql5);
$this->db->trans_complete();

return $this->db->trans_status();
}
  • 写回答

1条回答 默认 最新

  • dp7311 2015-06-08 16:51
    关注

    There are a couple of answers in SO exactly about your problem:

    Move node in nested set

    Move node in Nested Sets tree

    About the time your delete method is taking to run, 30ms it's very little for this kind of operation, so there's nothing to worry about. Don't fall into the trap of premature optimization. :)

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

报告相同问题?

悬赏问题

  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备