Actions

API16

JDatabase/getInstance

From Joomla! Documentation

< API16:JDatabase
Revision as of 21:58, 12 May 2013 by JoomlaWikiBot (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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

Returns the global Database object, only creating it if it doesn't already exist.

[<! removed edit link to red link >]

<! removed transcluded page call, red link never existed >

Syntax

static getInstance($options=array())
Parameter Name Default Value Description
$options array() Parameters to be passed to the database driver

Returns

A database object 

Defined in

libraries/joomla/database/database.php

Importing

jimport( 'joomla.database.database' );

Source Body

public static function getInstance($options = array())
{
        static $instances;
 
        if (!isset($instances)) {
                $instances = array();
        }
 
        $signature = serialize($options);
 
        if (empty($instances[$signature])) {
                $driver         = array_key_exists('driver', $options)          ? $options['driver']    : 'mysql';
                $select         = array_key_exists('select', $options)          ? $options['select']    : true;
                $database       = array_key_exists('database', $options)        ? $options['database']  : null;
 
                $driver = preg_replace('/[^A-Z0-9_\.-]/i', '', $driver);
                $path   = dirname(__FILE__).DS.'database'.DS.$driver.'.php';
 
                if (file_exists($path)) {
                        require_once $path;
                } else {
                        JError::setErrorHandling(E_ERROR, 'die'); //force error type to die
                        return JError::raiseError(500, JTEXT::_('UNABLE_TO_LOAD_DATABASE_DRIVER:') .$driver);
                }
 
                $adapter        = 'JDatabase'.$driver;
                $instance       = new $adapter($options);
 
                if ($error = $instance->getErrorMsg()) {
                        JError::setErrorHandling(E_ERROR, 'ignore'); //force error type to die
                        return JError::raiseError(500, JTEXT::_('UNABLE_TO_CONNECT_TO_THE_DATABASE') .$error);
                }
 
                $instances[$signature] = & $instance;
        }
 
        return $instances[$signature];
}

[<! removed edit link to red link >] <! removed transcluded page call, red link never existed >

Examples

<CodeExamplesForm />