API16

API16:JTable/load

From Joomla! Documentation

Revision as of 22:41, 22 March 2010 by Doxiki (talk | contribs) (New page: ===Description=== Method to load a row from the database by primary key and bind the fields to the JTable instance properties. <span class="editsection" style="font-size:76%;"> <nowik...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Description

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

[Edit Descripton]

Template: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] Template:SeeAlso:JTable/load

Examples

<CodeExamplesForm />