API15

JDatabase/getInstance: Difference between revisions

From Joomla! Documentation

Doxiki (talk | contribs)
New page: ===Description=== Returns a reference to the global Database object, only creating it if it doesn't already exist. <span class="editsection" style="font-size:76%;"> <nowiki>[</nowiki>...
 
m removing red link to edit, no existant pages
Line 3: Line 3:


<span class="editsection" style="font-size:76%;">
<span class="editsection" style="font-size:76%;">
<nowiki>[</nowiki>[[Description:JDatabase/getInstance|Edit Descripton]]<nowiki>]</nowiki>
<nowiki>[<! removed edit link to red link >]</nowiki>
</span>
</span>


{{Description:JDatabase/getInstance}}
<! removed transcluded page call, red link never existed >


===Syntax===
===Syntax===
Line 78: Line 78:


<span class="editsection" style="font-size:76%;">
<span class="editsection" style="font-size:76%;">
<nowiki>[</nowiki>[[SeeAlso:JDatabase/getInstance|Edit See Also]]<nowiki>]</nowiki>
<nowiki>[<! removed edit link to red link >]</nowiki>
</span>
</span>
{{SeeAlso:JDatabase/getInstance}}
<! removed transcluded page call, red link never existed >


===Examples===
===Examples===
Line 93: Line 93:
  format= ,,,
  format= ,,,
</dpl>
</dpl>
[[Category:Archived pages API15]]

Revision as of 13:55, 12 May 2013

Description

Returns a reference to 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

& 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

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
                        $error = JError::raiseError( 500, JTEXT::_('Unable to load Database Driver:') .$driver);
                        return $error;
                }

                $adapter        = 'JDatabase'.$driver;
                $instance       = new $adapter($options);

                if ( $error = $instance->getErrorMsg() )
                {
                        JError::setErrorHandling(E_ERROR, 'ignore'); //force error type to die
                        $error = JError::raiseError( 500, JTEXT::_('Unable to connect to the database:') .$error);
                        return $error;
                }


                $instances[$signature] = & $instance;
        }

        return $instances[$signature];
}

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

Examples

<CodeExamplesForm />