Path

ez components / documentation / api reference / 2008.2.1 / database


eZ Components 2008.2.1

Database: ezcDbInstance

[ Tutorial ] [ Oracle setup ] [ Class tree ] [ Element index ] [ ChangeLog ] [ Credits ]

Class: ezcDbInstance

Holds database instances for global access throughout an application. [source]
It is common for many application to use only one or very few database connections. This class holds a single database connection name or a list of databases identified by a handle. The database connections can be retrieved from anywhere within your code through the static methods. This eliminates the need to pass the connection handle around.
Typical usage example:
1.  $db ezcDbFactory::create$dbparams );
2.  ezcDbInstance::set$db );
3.  
4.  // ...
5.  
5.  
6.  $db ezcDbInstance::get();
More complex example, with two connections, having identifiers (for convenience):
 1.  $mydb ezcDbFactory::create$mysql_dbparams );
 2.  $pgdb ezcDbFactory::create$pgsql_dbparams );
 3.  
 4.  ezcDbInstance::set$mydb'my' );
 5.  ezcDbInstance::set$pgdb'pg' );
 6.  ezcDbInstance::chooseDefault'my' );
 7.  
 8.  // ...
 9.  
 9.  
10.  $mydb ezcDbInstance::get'my' )// returns the mysql instance
11.   $pgdb ezcDbInstance::get'pg' )// returns the pgsql instance
11.   $mydb ezcDbInstance::get();  // returns the mysql instance which is default

Method Summary

public static void chooseDefault( $identifier )
Sets the database $identifier as default database instance.
public static ezcDbHandler get( [$identifier = false] )
Returns the database instance $identifier.
public static array(string) getIdentifiers( )
Returns the identifiers of the registered database instances.
public static void reset( )
Resets this object to its initial state.
public static void resetDefault( )
Resets the default instance holder.
public static void set( $db, [$identifier = false] )
Adds the database handler $db to the list of known instances.

Methods

chooseDefault

void chooseDefault( string $identifier )
Sets the database $identifier as default database instance.
To retrieve the default database instance call get() with no parameters..

Parameters

Name Type Description
$identifier string  

See also:

ezcDbInstance::get()..


get

ezcDbHandler get( [string $identifier = false] )
Returns the database instance $identifier.
If $identifier is ommited the default database instance specified by chooseDefault() is returned.

Parameters

Name Type Description
$identifier string  

Throws

ClassDescription
ezcDbHandlerNotFoundException if the specified instance is not found.

getIdentifiers

array(string) getIdentifiers( )
Returns the identifiers of the registered database instances.

reset

void reset( )
Resets this object to its initial state.
The list of instances will be emptied and resetDefault() will be called.

resetDefault

void resetDefault( )
Resets the default instance holder.

set

void set( ezcDbHandler $db, [string $identifier = false] )
Adds the database handler $db to the list of known instances.
If $identifier is specified the database instance can be retrieved later using the same identifier.

Parameters

Name Type Description
$db ezcDbHandler  
$identifier string the identifier of the database handler

Last updated: Mon, 09 Feb 2009