API16

JTableNested/delete

From Joomla! Documentation

< API16:JTableNested

The "API16" namespace is an archived namespace. This page contains information for a Joomla! version which is no longer supported. It exists only as a historical reference, it will not be improved and its content may be incomplete and/or contain broken links.

Description[edit]

Method to delete a node, and optionally its child nodes, from the table.



Syntax[edit]

delete($pk=null, $children=true)
Parameter Name Default Value Description
$pk null The primary key of the node to delete.
$children true True to delete child nodes, false to move them up a level.

Returns[edit]

boolean True on success.

Defined in[edit]

libraries/joomla/database/tablenested.php

Importing[edit]

jimport( 'joomla.database.tablenested' );

Source Body[edit]

public function delete($pk = null, $children = true)
{
        // Initialise variables.
        $k = $this->_tbl_key;
        $pk = (is_null($pk)) ? $this->$k : $pk;

        // Lock the table for writing.
        if (!$this->_lock()) {
                // Error message set in lock method.
                return false;
        }

        // Get the node by id.
        if (!$node = $this->_getNode($pk)) {
                // Error message set in getNode method.
                $this->_unlock();
                return false;
        }

        // Should we delete all children along with the node?
        if ($children) {
                // Delete the node and all of its children.
                $this->_db->setQuery(
                        'DELETE FROM `'.$this->_tbl.'`' .
                        ' WHERE `lft` BETWEEN '.(int) $node->lft.' AND '.(int) $node->rgt
                );
                $this->_db->query();

                // Check for a database error.
                if ($this->_db->getErrorNum()) {
                        $this->setError($this->_db->getErrorMsg());
                        $this->_unlock();
                        return false;
                }

                // Compress the left values.
                $this->_db->setQuery(
                        'UPDATE `'.$this->_tbl.'`' .
                        ' SET `lft` = `lft` - '.(int) $node->width .
                        ' WHERE `lft` > '.(int) $node->rgt
                );
                $this->_db->query();

                // Check for a database error.
                if ($this->_db->getErrorNum()) {
                        $this->setError($this->_db->getErrorMsg());
                        $this->_unlock();
                        return false;
                }

                // Compress the right values.
                $this->_db->setQuery(
                        'UPDATE `'.$this->_tbl.'`' .
                        ' SET `rgt` = `rgt` - '.(int) $node->width .
                        ' WHERE `rgt` > '.(int) $node->rgt
                );
                $this->_db->query();

                // Check for a database error.
                if ($this->_db->getErrorNum()) {
                        $this->setError($this->_db->getErrorMsg());
                        $this->_unlock();
                        return false;
                }
        }

        // Leave the children and move them up a level.
        else {
                // Delete the node.
                $this->_db->setQuery(
                        'DELETE FROM `'.$this->_tbl.'`' .
                        ' WHERE `lft` = '.(int) $node->lft
                );
                $this->_db->query();

                // Check for a database error.
                if ($this->_db->getErrorNum()) {
                        $this->setError($this->_db->getErrorMsg());
                        $this->_unlock();
                        return false;
                }

                // Shift all node's children up a level.
                $this->_db->setQuery(
                        'UPDATE `'.$this->_tbl.'`' .
                        ' SET `lft` = `lft` - 1,' .
                        '       `rgt` = `rgt` - 1,' .
                        '       `level` = `level` - 1' .
                        ' WHERE `lft` BETWEEN '.(int) $node->lft.' AND '.(int) $node->rgt
                );
                $this->_db->query();

                // Check for a database error.
                if ($this->_db->getErrorNum()) {
                        $this->setError($this->_db->getErrorMsg());
                        $this->_unlock();
                        return false;
                }

                // Adjust all the parent values for direct children of the deleted node.
                $this->_db->setQuery(
                        'UPDATE `'.$this->_tbl.'`' .
                        ' SET `parent_id` = '.(int) $node->parent_id .
                        ' WHERE `parent_id` = '.(int) $node->$k
                );
                $this->_db->query();

                // Check for a database error.
                if ($this->_db->getErrorNum()) {
                        $this->setError($this->_db->getErrorMsg());
                        $this->_unlock();
                        return false;
                }

                // Shift all of the left values that are right of the node.
                $this->_db->setQuery(
                        'UPDATE `'.$this->_tbl.'`' .
                        ' SET `lft` = `lft` - 2' .
                        ' WHERE `lft` > '.(int) $node->rgt
                );
                $this->_db->query();

                // Check for a database error.
                if ($this->_db->getErrorNum()) {
                        $this->setError($this->_db->getErrorMsg());
                        $this->_unlock();
                        return false;
                }

                // Shift all of the right values that are right of the node.
                $this->_db->setQuery(
                        'UPDATE `'.$this->_tbl.'`' .
                        ' SET `rgt` = `rgt` - 2' .
                        ' WHERE `rgt` > '.(int) $node->rgt
                );
                $this->_db->query();

                // Check for a database error.
                if ($this->_db->getErrorNum()) {
                        $this->setError($this->_db->getErrorMsg());
                        $this->_unlock();
                        return false;
                }
        }

        // Unlock the table for writing.
        $this->_unlock();

        return true;
}



Examples[edit]

Code Examples[edit]