Back to Table class

Method getNextOrder

public int
getNextOrder
(mixed $where = '')
Method to get the next ordering value for a group of rows defined by an SQL WHERE clause.
Parameters
  • string $where WHERE clause to use for selecting the MAX(ordering) for the table.
Returns
  • int The next ordering value.
Since
  • 1.7.0
-
  • \UnexpectedValueException
Class: Table
Project: Joomla

Method getNextOrder - Source code

/**
 * Method to get the next ordering value for a group of rows defined by an SQL WHERE clause.
 *
 * This is useful for placing a new item last in a group of items in the table.
 *
 * @param   string  $where  WHERE clause to use for selecting the MAX(ordering) for the table.
 *
 * @return  integer  The next ordering value.
 *
 * @since   1.7.0
 * @throws  \UnexpectedValueException
 */
public function getNextOrder($where = '')
{
    // Check if there is an ordering field set
    if (!$this->hasField('ordering')) {
        throw new \UnexpectedValueException(sprintf('%s does not support ordering.', \get_class($this)));
    }
    // Get the largest ordering value for a given where clause.
    $query = $this->_db->getQuery(true)->select('MAX(' . $this->_db->quoteName($this->getColumnAlias('ordering')) . ')')->from($this->_tbl);
    if ($where) {
        $query->where($where);
    }
    $this->_db->setQuery($query);
    $max = (int) $this->_db->loadResult();
    // Return the largest ordering value + 1.
    return $max + 1;
}