Actions

API16

JTable/load

From Joomla! Documentation

< API16:JTable
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 load a row from the database by primary key and bind the fields to the JTable instance properties.

[Edit Descripton]

Description:JTable/load

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);
}

[Edit See Also] SeeAlso:JTable/load

Examples

<CodeExamplesForm />