Back to AdminModel class

Method getItem

public \Joomla\CMS\Object\CMSObject|bool
getItem
(mixed $pk = null)
Method to get a single record.
Parameters
  • int $pk The id of the primary key.
Returns
  • \Joomla\CMS\Object\CMSObject|bool Object on success, false on failure.
Since
  • 1.6
Class: AdminModel
Project: Joomla

Method getItem - Source code

/**
 * Method to get a single record.
 *
 * @param   integer  $pk  The id of the primary key.
 *
 * @return  CMSObject|boolean  Object on success, false on failure.
 *
 * @since   1.6
 */
public function getItem($pk = null)
{
    $pk = !empty($pk) ? $pk : (int) $this->getState($this->getName() . '.id');
    $table = $this->getTable();
    if ($pk > 0) {
        // Attempt to load the row.
        $return = $table->load($pk);
        // Check for a table object error.
        if ($return === false) {
            // If there was no underlying error, then the false means there simply was not a row in the db for this $pk.
            if (!$table->getError()) {
                $this->setError(Text::_('JLIB_APPLICATION_ERROR_NOT_EXIST'));
            } else {
                $this->setError($table->getError());
            }
            return false;
        }
    }
    // Convert to the CMSObject before adding other data.
    $properties = $table->getProperties(1);
    $item = ArrayHelper::toObject($properties, CMSObject::class);
    if (property_exists($item, 'params')) {
        $registry = new Registry($item->params);
        $item->params = $registry->toArray();
    }
    return $item;
}