Source code for file /libraries/joomla/application/component/model.php
Documentation is available at model.php
* @package Joomla.Platform
* @subpackage Application
* @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
* Base class for a Joomla Model
* Acts as a Factory class for application specific objects and
* provides many supporting API functions.
* @package Joomla.Platform
* @subpackage Application
* Indicates if the internal state has been set
* Indicates if the internal state has been set
* @deprecated use $stateSet declare as private
* @deprecated use $db declare as private
* @note Replaces _name variable in 11.1
* The URL option for the component.
* @note Replaces _state variable in 11.1
* The event to trigger when cleaning cache.
* Add a directory where JModel should search for models. You may
* either pass a string or an array of directories.
* @param mixed $path A path or array[sting] of paths to search.
* @param string $prefix A prefix for models.
* @return array An array with directory elements. If prefix is equal to '', all directories are returned.
if (!isset
($paths[$prefix]))
$paths[$prefix] =
array();
* Adds to the stack of model table paths in LIFO order.
* @param mixed $path The directory as a string or directories as an array to add.
* Create the filename for a resource
* @param string $type The resource type to create the filename for.
* @param array $parts An associative array of filename information.
* @return string The filename
* Returns a Model object, always creating it
* @param string $type The model type to instantiate
* @param string $prefix Prefix for the model class name. Optional.
* @param array $config Configuration array for model. Optional.
* @return mixed A model object or false on failure
public static function getInstance($type, $prefix =
'', $config =
array())
$modelClass =
$prefix .
ucfirst($type);
return new $modelClass($config);
* @param array $config An array of configuration options (name, state, dbo, table_path, ignore_request).
// Guess the option from the class name (Option)Model(View).
$this->name =
$config['name'];
$this->state =
$config['state'];
$this->_db =
$config['dbo'];
// Set the default view search path
elseif (defined('JPATH_COMPONENT_ADMINISTRATOR'))
$this->addTablePath(JPATH_COMPONENT_ADMINISTRATOR .
'/tables');
// Set the internal state marker - used to ignore setting state from the request
if (!empty($config['ignore_request']))
// Set the clean cache event
if (isset
($config['event_clean_cache']))
* Gets an array of objects from the results of database query.
* @param string $query The query.
* @param integer $limitstart Offset.
* @param integer $limit The number of records.
* @return array An array of results.
protected function _getList($query, $limitstart =
0, $limit =
0)
$this->_db->setQuery($query, $limitstart, $limit);
$result =
$this->_db->loadObjectList();
* Returns a record count for the query
* @param string $query The query.
* @return integer Number of rows for query
$this->_db->setQuery($query);
return $this->_db->getNumRows();
* Method to load and return a model object.
* @param string $name The name of the view
* @param string $prefix The class prefix. Optional.
* @param array $config Configuration settings to pass to JTable::getInstance
* @return mixed Model object or boolean false if failed
* @see JTable::getInstance
protected function _createTable($name, $prefix =
'Table', $config =
array())
// Make sure we are returning a DBO object
$config['dbo'] =
$this->getDbo();
* Method to get the database connector object
* @return JDatabase JDatabase connector object
* Method to get the model name
* The model name. By default parsed using the classname or it can be set
* by passing a $config['name'] in the class constructor
* @return string The name of the model
* Method to get model state variables
* @param string $property Optional parameter name
* @param mixed $default Optional default value
* @return object The property where specified, the state object where omitted
public function getState($property =
null, $default =
null)
// Protected method to auto-populate the model state.
// Set the model state set flag to true.
return $property ===
null ?
$this->state :
$this->state->get($property, $default);
* Method to get a table object, load it if necessary.
* @param string $name The table name. Optional.
* @param string $prefix The class prefix. Optional.
* @param array $options Configuration array for model. Optional.
* @return JTable A JTable object
public function getTable($name =
'', $prefix =
'Table', $options =
array())
* Method to auto-populate the model state.
* This method should only be called once per instantiation and is designed
* to be called on the first call to the getState() method unless the model
* configuration flag to ignore the request is set.
* @note Calling getState in this method will result in recursion.
* Method to set the database connector object
* @param object &$db A JDatabase based object
* Method to set model state variables
* @param string $property The name of the property.
* @param mixed $value The value of the property to set or null.
* @return mixed The previous value of the property or null if not set.
public function setState($property, $value =
null)
return $this->state->set($property, $value);
* @param string $group The cache group
* @param integer $client_id The ID of the client
protected function cleanCache($group =
null, $client_id =
0)
'cachebase' =>
($client_id) ?
JPATH_ADMINISTRATOR .
'/cache' :
$conf->get('cache_path', JPATH_SITE .
'/cache'));
// Trigger the onContentCleanCache event.