2012-09-21 2 views
2

PHP와 MYSQL을 사용하여 중첩 모델 세트에서 노드를 삭제하는 올바른 방법은 무엇입니까? 늘어나는만큼 내가 우려하고, 나는 왼쪽 값을 2. 노드 삭제보다 큰 모든 왼쪽 값을 감소해야합니다. 그러나 나는 다음 단계와 혼동합니다. 당신의 대답은 대단히 감사하겠습니다.PHP와 MYSQL을 사용하여 중첩 모델 세트에서 노드를 삭제하는 방법

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

} 

답변

1

난 그냥 마이크 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; 
     }  
    } 

} 
+0

에서 해결책을 찾아 삭제 대상 아동이있는 경우 첫 번째 쿼리를 확인합니다 :

내 기능입니다. 자식이 있으면 다른 하나를 사용하지 않으면 else 문에서 트랜잭션을 사용합니다. –

관련 문제