phpDBTree: рекурсивное удаление записей связанных таблиц

_RVK_

Новичок
phpDBTree: рекурсивное удаление записей связанных таблиц

Автор класса советует хранить данные отдельно от дерева, но класс не предоставляет средств для удаления записей из таких таблиц, даже для связей один - к - одному.

Решил что это неправильно и немного подправил методы delete и deleteAll.

Привожу методы полностью(сори за большой код), кому нужно, можно просто заменить существующие.
Оцените, плиз. Имхо, полезная фича.
PHP:
//************************************************************************
// Deletes a record wihtout deleting its children
// $ID : an ID of the element to be deleted
// $tables : array of related tables in format 'table_name'=>'foreign_key',... 
// if level to nesting of tables more 2, that array format by 'table_name'=>array('foreign_key',array('table_name1'=>'foreign_key1',...))
// Returns : true on success, or false on error
	function delete($ID,$tables=array()) {
		if(!(list($leftId, $rightId, $level) = $this->getNodeInfo($ID))) die("phpDbTree error on line ".__LINE__.": ".$this->db->error());

        // Deleting record
        $this->_delete_records($ID,$tables);
     	$this->sql = 'DELETE FROM '.$this->table.' WHERE '.$this->id.'=\''.$ID.'\'';
	    if(!$this->db->query($this->sql)) die("phpDbTree error on line ".__LINE__.": ".$this->db->error());
		
        // Clearing blank spaces in a tree
		$this->sql = 'UPDATE '.$this->table.' SET '
			. $this->left.'=IF('.$this->left.' BETWEEN '.$leftId.' AND '.$rightId.','.$this->left.'-1,'.$this->left.'),'
			. $this->right.'=IF('.$this->right.' BETWEEN '.$leftId.' AND '.$rightId.','.$this->right.'-1,'.$this->right.'),'
			. $this->level.'=IF('.$this->left.' BETWEEN '.$leftId.' AND '.$rightId.','.$this->level.'-1,'.$this->level.'),'
			. $this->left.'=IF('.$this->left.'>'.$rightId.','.$this->left.'-2,'.$this->left.'),'
			. $this->right.'=IF('.$this->right.'>'.$rightId.','.$this->right.'-2,'.$this->right.') '
			. 'WHERE '.$this->right.'>'.$leftId
		;
		if(!$this->db->query($this->sql)) die("phpDbTree error on line ".__LINE__.": ".$this->db->error());

		return true;
	}

//*************************************************************************************
//(Private) Recursive deleting records from related tables
// $ID : an ID of the element to be deleted
// $tables : array of related tables in format 'table_name'=>'foreign_key'.
// Returns : true on success
    function _delete_records($ID,$tables)
    {
        if (is_array($tables) && count($tables))
        {
            foreach($tables as $key=>$value)
            {
               if (!is_array($value))
               {
                  //Deleting records
                  $this->sql = 'DELETE FROM '.$key.' WHERE '.$value.'="'.$ID.'"';
                  if(!$this->db->query($this->sql)) die("phpDbTree error on line ".__LINE__.": ".$this->db->error());
               }
               else
               {
                  $this->sql = 'SELECT id FROM '.$key.' WHERE '.$value[0].'="'.$ID.'"';
                  if(!$query = $this->db->query($this->sql)) die("phpDbTree error on line ".__LINE__.": ".$this->db->error());
                  while ($data = $this->db->fetch_array($query))  { 
                      $this->_delete_records($data['id'],$value[1]); 
                      $this->sql = 'DELETE FROM '.$key.' WHERE '.$value['0'].'="'.$ID.'"';
                      if(!$this->db->query($this->sql)) die("phpDbTree error on line ".__LINE__.": ".$this->db->error());
                  }
               }
             }
         }
	
    	return true;
     }

//************************************************************************
// Deletes a record with all its children
// $ID : an ID of the element to be deleted
// Returns : true on success, or false on error
	function deleteAll($ID,$tables=array()) {
		if(!(list($leftId, $rightId, $level) = $this->getNodeInfo($ID))) die("phpDbTree error on line ".__LINE__.": ".$this->db->error());

        // Deleteing record(s)
        if (is_array($tables) && count($tables)) {
		    $this->sql = 'SELECT id FROM '.$this->table.' WHERE '.$this->left.' BETWEEN '.$leftId.' AND '.$rightId.' ORDER BY cleft DESC';
		    if(!$query = $this->db->query($this->sql)) die("phpDbTree error on line ".__LINE__.": ".$this->db->error());
            while ($data = $this->db->fetch_array($query))  { 
                $this->_delete_records($data['id'],$tables);
     	        $this->sql = 'DELETE FROM '.$this->table.' WHERE '.$this->id.'=\''.$data['id'].'\'';
                if(!$this->db->query($this->sql)) die("phpDbTree error on line ".__LINE__.": ".$this->db->error());
            }
        }
        else {
		    $this->sql = 'DELETE FROM '.$this->table.' WHERE '.$this->left.' BETWEEN '.$leftId.' AND '.$rightId;
		    if(!$this->db->query($this->sql)) die("phpDbTree error on line ".__LINE__.": ".$this->db->error());
        }

		// Clearing blank spaces in a tree
		$deltaId = ($rightId - $leftId)+1;
		$this->sql = 'UPDATE '.$this->table.' SET '
			. $this->left.'=IF('.$this->left.'>'.$leftId.','.$this->left.'-'.$deltaId.','.$this->left.'),'
			. $this->right.'=IF('.$this->right.'>'.$leftId.','.$this->right.'-'.$deltaId.','.$this->right.') '
			. 'WHERE '.$this->right.'>'.$rightId
		;
		if(!$this->db->query($this->sql)) die("phpDbTree error on line ".__LINE__.": ".$this->db->error());

		return true;
	}
 
Сверху