我正在尝试使用SQLite存储分层数据。经过大量搜索之后,我选择使用嵌套集合模型而不是邻接表,因为几乎90%的操作将被读取,而只有10%的操作将被更新/删除/创建。
我遵循以下示例:http : //www.phpro.org/tutorials/Managing-Hierarchical-Data-with-PHP-and-MySQL.html
它可以很好地添加,删除和读取新节点。
但是我没有找到任何文章解释如何更新树,例如将一个类别移动到另一个类别。
下面是我的数据库结构:
id name left_node right_node
1 name1 1 2
**我没有找到解释如何更新层次结构的地方,这是我真正需要的。**
另一个问题是
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();
}
这是我的删除方法,需要30毫秒才能完成。那是正常的吗?
我解决了问题,感谢您的帮助http://www.ninthavenue.com.au/how-to-move-a-node-in-nested-sets-with-sql
我在sqlite数据库中使用codeigniter。下面是我的功能,
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();
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句