J2.5:Accessing the database using JDatabase

From Joomla! Documentation
(Difference between revisions)
Jump to: navigation, search
m
(Usage: Connection usage.)
Line 4: Line 4:
  
 
==Usage==
 
==Usage==
 +
 +
Joomla can use different kinds of SQL database systems and run in a variety of environments with different table-prefixes. In addition to these functions, the class automatically creates the database connection. Besides instantiating the object you need just two lines of code to get a result from the database in a variety of formats. Using the Joomla database layer ensures a maximum of compatibility and flexibility for your extension.
  
 
===The Connection===
 
===The Connection===
 +
 +
Joomla supports a generic interface for connecting to the database, hiding the intricacies of a specific database server from the Framework developer, thus making Joomla code easier to port from one database to another.
 +
 +
To obtain a database connection, use the JFactory getDbo static method:
 +
 +
<source lang="php">
 +
$db = JFactory::getDbo();
 +
</source>
 +
 +
Alternatively, if your class inherits from the JModel (or a decendent class) getDbo method:
 +
 +
<source lang="php">
 +
$db = $this->getDbo();
 +
</source>
 +
 +
{{JVer|3.0}} users should note that the JModel class has been renamed JModelLegacy and will eventually be deprecated in favour of the class JModelBase. Therefore, any Joomla 3.0 core or extension code should use JFactory::getDbo() to obtain a database connection.
  
 
===The Query===
 
===The Query===
 +
 +
Joomla's database querying has changed since the new Joomla Framework was introduced  "query chaining" is now the recommended method for building database queries.

Revision as of 01:21, 16 November 2012

Joomla provides a sophisticated database abstraction layer to simplify the usage for third party developers. New versions of the Joomla Platform API provide additional functionality which extends the database layer further, and includes features such as connectors to a greater variety of database servers and the query chaining to improve readability of connection code and simplify SQL coding.

Usage

Joomla can use different kinds of SQL database systems and run in a variety of environments with different table-prefixes. In addition to these functions, the class automatically creates the database connection. Besides instantiating the object you need just two lines of code to get a result from the database in a variety of formats. Using the Joomla database layer ensures a maximum of compatibility and flexibility for your extension.

The Connection

Joomla supports a generic interface for connecting to the database, hiding the intricacies of a specific database server from the Framework developer, thus making Joomla code easier to port from one database to another.

To obtain a database connection, use the JFactory getDbo static method:

$db = JFactory::getDbo();

Alternatively, if your class inherits from the JModel (or a decendent class) getDbo method:

$db = $this->getDbo();

Joomla 3.0 users should note that the JModel class has been renamed JModelLegacy and will eventually be deprecated in favour of the class JModelBase. Therefore, any Joomla 3.0 core or extension code should use JFactory::getDbo() to obtain a database connection.

The Query

Joomla's database querying has changed since the new Joomla Framework was introduced "query chaining" is now the recommended method for building database queries.

Personal tools
Namespaces

Variants
Actions
Navigation
Joomla! Sites
Toolbox