API15

API15:JTable/getInstance

From Joomla! Documentation

Description

Returns a reference to the a Table object, always creating it

[<! removed edit link to red link >]

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

Syntax

& getInstance($type, $prefix= 'JTable', $config=array())
Parameter Name Default Value Description
$type $type The table type to instantiate
$prefix $prefix A prefix for the table class name. Optional.
$config array() $options Configuration array for model. Optional.

Returns

database A database object

Defined in

libraries/joomla/database/table.php

Importing

jimport( 'joomla.database.table' );

Source Body

function &getInstance( $type, $prefix = 'JTable', $config = array() )
{
        $false = false;

        $type = preg_replace('/[^A-Z0-9_\.-]/i', '', $type);
        $tableClass = $prefix.ucfirst($type);

        if (!class_exists( $tableClass ))
        {
                jimport('joomla.filesystem.path');
                if($path = JPath::find(JTable::addIncludePath(), strtolower($type).'.php'))
                {
                        require_once $path;

                        if (!class_exists( $tableClass ))
                        {
                                JError::raiseWarning( 0, 'Table class ' . $tableClass . ' not found in file.' );
                                return $false;
                        }
                }
                else
                {
                        JError::raiseWarning( 0, 'Table ' . $type . ' not supported. File not found.' );
                        return $false;
                }
        }

        //Make sure we are returning a DBO object
        if (array_key_exists('dbo', $config))  {
                $db =& $config['dbo'];
        } else {
                $db = & JFactory::getDBO();
        }

        $instance = new $tableClass($db);
        //$instance->setDBO($db);

        return $instance;
}

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

Examples

Code Examples