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.


Method to set the publishing state for a node or list of nodes in the database table. The method respects rows checked out by other users and will attempt to checkin rows that it can after adjustments are made. The method will now allow you to set a publishing state higher than any ancestor node and will not allow you to set a publishing state on a node with a checked out child.


publish($pks=null, $state=1, $userId=0)
Parameter Name Default Value Description
$pks null An optional array of primary key values to update. If not set the instance property value is used.
$state 1 The publishing state. eg. [0 = unpublished, 1 = published]
$userId 0 The user id of the user performing the operation.


boolean True on success.

Defined in[edit]



jimport( 'joomla.database.tablenested' );

Source Body[edit]

public function publish($pks = null, $state = 1, $userId = 0)
        // Initialise variables.
        $k = $this->_tbl_key;

        // Sanitize input.
        $userId = (int) $userId;
        $state  = (int) $state;

        // If there are no primary keys set check to see if the instance key is set.
        if (empty($pks)) {
                if ($this->$k) {
                        $pks = array($this->$k);
                // Nothing to set publishing state on, return false.
                else {
                        return false;

        // Determine if there is checkout support for the table.
        if (!property_exists($this, 'checked_out') || !property_exists($this, 'checked_out_time')) {
                $checkoutSupport = false;
        } else {
                $checkoutSupport = true;

        // Iterate over the primary keys to execute the publish action if possible.
        foreach ($pks as $pk) {
                // Get the node by primary key.
                if (!$node = $this->_getNode($pk)) {
                        // Error message set in getNode method.
                        return false;

                // If the table has checkout support, verify no children are checked out.
                if ($checkoutSupport) {
                        // Ensure that children are not checked out.
                                'SELECT COUNT('.$this->_tbl_key.')' .
                                ' FROM `'.$this->_tbl.'`' .
                                ' WHERE `lft` BETWEEN '.(int) $node->lft.' AND '.(int) $node->rgt .
                                ' AND (checked_out <> 0 AND checked_out <> '.(int) $userId.')'

                        // Check for checked out children.
                        if ($this->_db->loadResult()) {
                                return false;

                // If any parent nodes have lower published state values, we cannot continue.
                if ($node->parent_id) {
                        // Get any ancestor nodes that have a lower publishing state.
                                'SELECT p.'.$k .
                                ' FROM `'.$this->_tbl.'` AS n, `'.$this->_tbl.'` AS p' .
                                ' WHERE n.lft BETWEEN p.lft AND p.rgt' .
                                ' AND n.'.$k.' = '.(int) $pk .
                                ' AND p.parent_id > 0' .
                                ' AND p.published < '.(int) $state .
                                ' ORDER BY p.lft DESC',
                                1, 0
                        $rows = $this->_db->loadResultArray();

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

                        if (!empty($rows)) {
                                return false;

                // Update the publishing state.
                        'UPDATE `'.$this->_tbl.'`' .
                        ' SET `published` = '.(int) $state .
                        ' WHERE `'.$this->_tbl_key.'` = '.(int) $pk

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

                // If checkout support exists for the object, check the row in.
                if ($checkoutSupport) {

        // If the JTable instance value is in the list of primary keys that were set, set the instance.
        if (in_array($this->$k, $pks)) {
                $this->published = $state;

        $this->_errors = array();
        return true;


Code Examples[edit]