Actions

API16

JTable/load

< API16:JTable

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

Method to load a row from the database by primary key and bind the fields to the JTable instance properties.



Syntax

load($keys=null, $reset=true)
Parameter Name Default Value Description
$keys null An optional primary key value to load the row by, or an array of fields to match. If not set the instance property value is used.
$reset true True to reset the default values before loading the new row.

Returns

boolean True if successful. False if row not found or on error (internal error state set in that case).

Defined in

libraries/joomla/database/table.php

Importing

jimport( 'joomla.database.table' );

Source Body

public function load($keys = null, $reset = true)
{
        if (empty($keys))
        {
                // If empty, use the value of the current key
                $keyName = $this->getKeyName();
                $keys = array($keyName => $this->$keyName);
        } else if (!is_array($keys)) {
                // Load by primary key.
                $keyName = $this->getKeyName();
                $keys = array($keyName => $keys);
        }
 
        if ($reset) {
                $this->reset();
        }
 
        // Initialise the query.
        $db             = $this->getDBO();
        $query  = $db->getQuery(true);
        $query->select('*');
        $query->from($this->getTableName());
        $fields = array_keys($this->getProperties());
 
        foreach ($keys as $field => $value) {
                // Check that $field is in the table.
                if (!in_array($field, $fields)) {
                        $this->setError(JText::sprintf('JTable_Error_Class_is_missing_field', get_class($this), $field));
                        return false;
                }
                // Add the search tuple to the query.
                $query->where($db->nameQuote($field).' = '.$db->quote($value));
        }
 
        $db->setQuery($query);
        $row = $db->loadAssoc();
 
        // Check for a database error.
        if ($db->getErrorNum()) {
                $this->setError($db->getErrorMsg());
                return false;
        }
 
        // Check that we have a result.
        if (empty($row)) {
                return false;
        }
 
        // Bind the object with the row and return.
        return $this->bind($row);
}



Examples

Code Examples


Advertisement