嵌套集添加点头得到错误
Nested Sets adding nod get error
我将 class 用于 php 嵌套集,当我想添加一个新的点头时出现错误,
Fatal error: Uncaught exception 'Exception' with message 'SafeMySQL: Duplicate entry '0' for key 'PRIMARY'. Full query: [INSERT INTO test_sections SET `section_name`='accebt',`section_left`='55',`section_right`='56',`section_level`='1']' in C:\xampp\htdocs\easy_b\shop_admin\db_tree\safemysql.class.php:607 Stack trace: #0 C:\xampp\htdocs\easy_b\shop_admin\db_tree\safemysql.class.php(429): SafeMySQL->error('Duplicate entry...') #1 C:\xampp\htdocs\easy_b\shop_admin\db_tree\safemysql.class.php(59): SafeMySQL->rawQuery('INSERT INTO tes...') #2 C:\xampp\htdocs\easy_b\shop_admin\db_tree\DbTree.class.php(215): SafeMySQL->query('INSERT INTO ?p ...', 'test_sections', Array) #3 C:\xampp\htdocs\easy_b\shop_admin\db_tree\examples\dbtree_demo.php(236): DbTree->Insert(0, Array) #4 {main} thrown in C:\xampp\htdocs\easy_b\shop_admin\db_tree\safemysql.class.php on line 607
我的代码看起来像这样,我搜索了错误但我什么也没找到,你能帮我吗?谢谢
public function Insert($parentId, $data = array(), $condition = '')
{
$node_info = $this->GetNode($parentId);
$right_id = $node_info[$this->tableRight];
$level = $node_info[$this->tableLevel];
$condition = $this->PrepareCondition($condition);
$sql = 'UPDATE ' . $this->table . ' SET ';
$sql .= $this->tableLeft . '=CASE WHEN ' . $this->tableLeft . '>' . $right_id . ' THEN ' . $this->tableLeft . '+2 ELSE ' . $this->tableLeft . ' END, ';
$sql .= $this->tableRight . '=CASE WHEN ' . $this->tableRight . '>=' . $right_id . ' THEN ' . $this->tableRight . '+2 ELSE ' . $this->tableRight . ' END ';
$sql .= 'WHERE ' . $this->tableRight . '>=' . $right_id;
$sql .= $condition;
$this->db->query($sql);
$data[$this->tableLeft] = $right_id;
$data[$this->tableRight] = $right_id + 1;
$data[$this->tableLevel] = $level + 1;
$sql = 'INSERT INTO ?p SET ?u';
$this->db->query($sql, $this->table, $data);
$node_id = $this->db->insertId();
return $node_id;
}
我自己解决了,问题出在数据库中,我必须自动增加 ID。谢谢
我将 class 用于 php 嵌套集,当我想添加一个新的点头时出现错误,
Fatal error: Uncaught exception 'Exception' with message 'SafeMySQL: Duplicate entry '0' for key 'PRIMARY'. Full query: [INSERT INTO test_sections SET `section_name`='accebt',`section_left`='55',`section_right`='56',`section_level`='1']' in C:\xampp\htdocs\easy_b\shop_admin\db_tree\safemysql.class.php:607 Stack trace: #0 C:\xampp\htdocs\easy_b\shop_admin\db_tree\safemysql.class.php(429): SafeMySQL->error('Duplicate entry...') #1 C:\xampp\htdocs\easy_b\shop_admin\db_tree\safemysql.class.php(59): SafeMySQL->rawQuery('INSERT INTO tes...') #2 C:\xampp\htdocs\easy_b\shop_admin\db_tree\DbTree.class.php(215): SafeMySQL->query('INSERT INTO ?p ...', 'test_sections', Array) #3 C:\xampp\htdocs\easy_b\shop_admin\db_tree\examples\dbtree_demo.php(236): DbTree->Insert(0, Array) #4 {main} thrown in C:\xampp\htdocs\easy_b\shop_admin\db_tree\safemysql.class.php on line 607
我的代码看起来像这样,我搜索了错误但我什么也没找到,你能帮我吗?谢谢
public function Insert($parentId, $data = array(), $condition = '')
{
$node_info = $this->GetNode($parentId);
$right_id = $node_info[$this->tableRight];
$level = $node_info[$this->tableLevel];
$condition = $this->PrepareCondition($condition);
$sql = 'UPDATE ' . $this->table . ' SET ';
$sql .= $this->tableLeft . '=CASE WHEN ' . $this->tableLeft . '>' . $right_id . ' THEN ' . $this->tableLeft . '+2 ELSE ' . $this->tableLeft . ' END, ';
$sql .= $this->tableRight . '=CASE WHEN ' . $this->tableRight . '>=' . $right_id . ' THEN ' . $this->tableRight . '+2 ELSE ' . $this->tableRight . ' END ';
$sql .= 'WHERE ' . $this->tableRight . '>=' . $right_id;
$sql .= $condition;
$this->db->query($sql);
$data[$this->tableLeft] = $right_id;
$data[$this->tableRight] = $right_id + 1;
$data[$this->tableLevel] = $level + 1;
$sql = 'INSERT INTO ?p SET ?u';
$this->db->query($sql, $this->table, $data);
$node_id = $this->db->insertId();
return $node_id;
}
我自己解决了,问题出在数据库中,我必须自动增加 ID。谢谢