API16

JTable/load: Difference between revisions

From Joomla! Documentation

m clean up
m preparing for archive only
 
Line 4: Line 4:




{{subst:Description:JTable/load}}
 


===Syntax===
===Syntax===
Line 88: Line 88:




{{subst:SeeAlso:JTable/load}}
 


===Examples===
===Examples===
<CodeExamplesForm />
=== Code Examples ===
<dpl>
<dpl>
  noresultsheader=\n
  noresultsheader=\n
  category=load
  category=load
  category=JTable
  category=JTable
  category=CodeExample
  namespace=CodeExample
  category=MethodExample
  category=MethodExample
  include=*
  include=*
  format= ,,,
  format= ,,,
</dpl>
</dpl>

Latest revision as of 02:09, 25 March 2017

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