Back to Table class

Method hasPrimaryKey

public bool
hasPrimaryKey
()
Validate that the primary key has been set.
Returns
  • bool True if the primary key(s) have been set.
Since
  • 3.1.4
Class: Table
Project: Joomla

Method hasPrimaryKey - Source code

/**
 * Validate that the primary key has been set.
 *
 * @return  boolean  True if the primary key(s) have been set.
 *
 * @since   3.1.4
 */
public function hasPrimaryKey()
{
    if ($this->_autoincrement) {
        $empty = true;
        foreach ($this->_tbl_keys as $key) {
            $empty = $empty && empty($this->{$key});
        }
    } else {
        $query = $this->_db->getQuery(true)->select('COUNT(*)')->from($this->_tbl);
        $this->appendPrimaryKeys($query);
        $this->_db->setQuery($query);
        $count = $this->_db->loadResult();
        if ($count == 1) {
            $empty = false;
        } else {
            $empty = true;
        }
    }
    return !$empty;
}