Actions

API16

JTable/move

From Joomla! Documentation

< API16:JTable
Revision as of 17:41, 22 March 2010 by Doxiki (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Replacement filing cabinet.png
This Namespace has been archived - Please Do Not Edit or Create Pages in this namespace. Pages contain information for a Joomla! version which is no longer supported. It exists only as a historical reference, will not be improved and its content may be incomplete.

Contents

Description

Method to move a row in the ordering sequence of a group of rows defined by an SQL WHERE clause. Negative numbers move the row up in the sequence and positive numbers move it down.

[Edit Descripton]

Description:JTable/move

Syntax

move($delta, $where= '')
Parameter Name Default Value Description
$delta The direction and magnitude to move the row in the ordering sequence.
$where WHERE clause to use for limiting the selection of rows to compact the ordering values.

Returns

mixed Boolean true on success.

Defined in

libraries/joomla/database/table.php

Importing

jimport( 'joomla.database.table' );

Source Body

public function move($delta, $where = '')
{
        // If there is no ordering field set an error and return false.
        if (!property_exists($this, 'ordering')) {
                $this->setError(get_class($this).' does not support ordering');
                return false;
        }
 
        // If the change is none, do nothing.
        if (empty($delta)) {
                return true;
        }
 
        // Initialise variables.
        $k = $this->_tbl_key;
        $row = null;
 
        $query  = $this->_db->getQuery(true);
 
        // Select the primary key and ordering values from the table.
        $query->select('`'.$this->_tbl_key.'`, `ordering`');
        $query->from('`'.$this->_tbl.'`');
 
        // If the movement delta is negative move the row up.
        if ($delta < 0) {
                $query->where('`ordering` < '.(int) $this->ordering);
                $query->order('`ordering` DESC');
        }
        // If the movement delta is positive move the row down.
        elseif ($delta > 0) {
                $query->where('`ordering` > '.(int) $this->ordering);
                $query->order('`ordering` ASC');
        }
 
        // Add the custom WHERE clause if set.
        if ($where) {
                $query->where($where);
        }
 
        // Select the first row with the criteria.
        $this->_db->setQuery((string) $query, 0, 1);
        $row = $this->_db->loadObject();
 
        // If a row is found, move the item.
        if (!empty($row)) {
                // Update the ordering field for this instance to the row's ordering value.
                $this->_db->setQuery(
                        'UPDATE `'.$this->_tbl.'`' .
                        ' SET `ordering` = '.(int) $row->ordering .
                        ' WHERE `'.$this->_tbl_key.'` = '.$this->_db->quote($this->$k)
                );
                $this->_db->query();
 
                // Check for a database error.
                if ($this->_db->getErrorNum()) {
                        $this->setError($this->_db->getErrorMsg());
                        return false;
                }
 
                // Update the ordering field for the row to this instance's ordering value.
                $this->_db->setQuery(
                        'UPDATE `'.$this->_tbl.'`' .
                        ' SET `ordering` = '.(int) $this->ordering .
                        ' WHERE `'.$this->_tbl_key.'` = '.$this->_db->quote($row->$k)
                );
                $this->_db->query();
 
                // Check for a database error.
                if ($this->_db->getErrorNum()) {
                        $this->setError($this->_db->getErrorMsg());
                        return false;
                }
 
                // Update the instance value.
                $this->ordering = $row->ordering;
        } else {
                // Update the ordering field for this instance.
                $this->_db->setQuery(
                        'UPDATE `'.$this->_tbl.'`' .
                        ' SET `ordering` = '.(int) $this->ordering .
                        ' WHERE `'.$this->_tbl_key.'` = '.$this->_db->quote($this->$k)
                );
                $this->_db->query();
 
                // Check for a database error.
                if ($this->_db->getErrorNum()) {
                        $this->setError($this->_db->getErrorMsg());
                        return false;
                }
        }
 
        return true;
}

[Edit See Also] SeeAlso:JTable/move

Examples

<CodeExamplesForm />